Sign In
Sign In

How to Set Up Visual Studio Code for Python

How to Set Up Visual Studio Code for Python
Hostman Team
Technical writer
Python
05.02.2025
Reading time: 10 min

Creating and debugging programs in Python is easier when using a specialized Integrated Development Environment (IDE). With an IDE, you can quickly and efficiently develop, test, and debug programs.

Visual Studio Code (VS Code) for Python provides full support for the language and offers a wide range of plugins and extensions. In this article, we will install Visual Studio Code on three operating systems (Windows, macOS, Linux) and set it up for Python programming, including the use of popular plugins.

Prerequisites

To install and set up Visual Studio Code for Python, we will need the following:

  • A personal or work computer with Windows 10/11, macOS, or Ubuntu Linux distribution version 24.04 pre-installed. Alternatively, you can rent a dedicated server or a virtual machine with Windows Server 2016/2019/2022. If using regular versions of Windows, you can download your own ISO image in advance. You can also rent a server with Ubuntu.

Installing the Python Interpreter

Before installing VS Code, we need to install the Python interpreter on all three operating systems — Windows, macOS, and Linux.

On Windows

  1. Go to the official Python website and download the installer file. In this case, we will be installing Python version 3.13.1.

F0a25261 3828 4bc2 923d 786a97e670be (1)

  1. Run the installer file.
  2. You will have two installation options:
    • Install Now — This performs a full installation, including documentation files, the package manager pip, the tcl/tk library for graphical interface support, and standard libraries.

    • Customize Installation — This option allows you to choose which components to install.

We will use the full installation. Make sure to check the box next to the option Add python.exe to PATH and click Install Now. The installation process will begin.

Image7

  1. Once the installation is complete, the program will notify you that it has finished.

Image27

On macOS

On macOS, the Python interpreter is pre-installed by default. You can verify this by running the following command in the terminal:

python3 --version

D4a7a3e0 283f 4b80 Bb4b 2324887d13ae

However, the installed version may be outdated. If necessary, you can install a newer version. To do this, we will use the Homebrew package manager. First, if Homebrew is not installed on your system, you can install it by running the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Next, you need to check which versions of Python are available for installation. Use the following command:

brew search python

In our case, several versions of Python are available:

Image22

Install the latest available version, Python 3.13, by running the following command:

brew install python@3.13

Check the Python version again:

python3 --version
python3.13 --version

Image5

As shown in the screenshot above, running the python3 --version still shows the old version (Python 3.9.6). However, the newly installed version (3.13) can be accessed using the command python3.13 --version. If needed, you can change the default Python version to the newly installed one. To do this, first get the full path to the newly installed Python interpreter using the following command:

brew --prefix python@3.13

Then, check which shell you are using:

echo $SHELL

Depending on the shell used, open the corresponding file for editing:

For bash or sh:

nano ~/.bashrc

For zsh:

nano ~/.zshrc

Add the following line at the end of the file:

export PATH="/opt/homebrew/opt/python@3.13/bin:$PATH"

Save the changes and reload the file:

source ~/.zshrc

Now, when you check the Python version, it will display the latest installed version:

python3 --version

Image26

On Ubuntu

By default, Python is pre-installed on almost all Linux distributions, including Ubuntu. In the latest supported versions of Ubuntu, the current version of Python is installed:

python3 --version

Image16

However, if Python is not installed for any reason, you can install it by running the following command:

apt -y install python3

Installing Visual Studio Code

You can install Visual Studio Code on your personal computer. You can also rent a dedicated or cloud server with Windows Server or one of the available Linux distributions. If the required distribution is not available in the list of offered images, you can upload your own.

We will cover the installation of Visual Studio Code on three operating systems: Windows, macOS, and Linux (Ubuntu 24.04 distribution).

For Windows

Visual Studio Code supports installation on Windows 10 and Windows 11. It also supports Windows Server distributions, from version 2016 to 2022.

We will install it on Windows 10. 

  1. Go to the official website and download the installer.

Image15

  1. This will download an .exe installation file. Run the installer file. 
  2. On the first step, accept the license agreement by selecting the option "I accept the agreement".

Image18

  1. Next, the installer will prompt you to choose an installation location. You can choose the default path suggested by the installer or specify your own.

Image11

  1. If necessary, you can create a shortcut for the program in the Windows menu. If you don’t want to create a shortcut, select the option "Don’t create a Start Menu folder" at the bottom:

Image3

  1. The next step lets you configure additional options by ticking the corresponding checkboxes:
    • Create a desktop icon — creates a shortcut on the desktop for quick access to the program.

    • Add “Open with Code” action to Windows Explorer file context menu — adds the "Open with Code" option to the context menu when right-clicking on a file. This option allows you to quickly open any file directly in Visual Studio Code.

    • Add “Open with Code” action to Windows Explorer directory file context menu — similar to the above option but adds the "Open with Code" option to the context menu of directories (folders).

    • Register Code as an editor for supported file types — makes Visual Studio Code the default editor for certain file types (e.g., .c, .cpp, .py, .java, .js, .html files).

    • Add to PATH (require shell restart) — adds Visual Studio Code to the system’s PATH variable so it can be launched from the command line (cmd).

Image20

  1. Once all necessary options are set, Visual Studio Code is ready for installation. Click Install.

After the installation is complete, you can launch the program immediately:

Image12

For macOS

  1. Go to the official website and download the installer:

Image33

  1. After downloading, you will have a ZIP archive. Inside the archive, you will find the executable file, which you need to extract to the "Applications" directory.
  2. On the first launch, the system will notify you that this file was downloaded from the internet and may not have vulnerabilities. Click Open to continue:

Image19

For Linux (Ubuntu)

Visual Studio Code supports installation on Linux distributions such as Ubuntu, Debian, Red Hat, Fedora, and SUSE. You need a graphical desktop environment to install Visual Studio Code on Linux (GNOME, KDE, Xfce, etc.).

Let’s consider the installation of Visual Studio Code on Ubuntu 24.04 with the Xfce desktop environment. You can also install Visual Studio Code using Snapcraft.

  1. Go to the official website and download the installer for your Linux distribution. In our case, we need the .deb installer:

Image37

  1. Once the file is downloaded, open a terminal (console) and navigate to the directory where the file was downloaded (e.g., /root).

  2. To install, run the following command where code_1.96.2-1734607745_amd64.deb is the name of the downloaded file:

dpkg -i code_1.96.2-1734607745_amd64.deb
  1. During installation, a message will prompt you to add Microsoft repositories to the system. Select <Yes> and press Enter:

Image39

  1. Wait for the installation to complete.

  2. Once the installation is finished, you can launch the program from the applications menu (for distributions using Xfce, Visual Studio Code is available in the menu: ApplicationsDevelopment):

Image23

Adding Python Interpreter to PATH Variable in Windows

If you haven't checked the Add python.exe to PATH checkbox during the Python installation on Windows, you need to manually add the full path to the interpreter to run Python from the command line.

To do this:

  1. Press Win+R, type sysdm.cpl in the Run window, and press Enter.
  2. In the window that opens, go to the Advanced tab and click on the Environment Variables button.

Image6

  1. To add a user-level variable, select the Path variable under User variables and click on Edit.

Image14

  1. Double-click on an empty field or click the New button.
  2. Enter the full path to the Python interpreter file. By default, the Python interpreter is located at the following path:
C:\Users\<Username>\AppData\Local\Programs\Python\Python313

For example:

C:\Users\Administrator\AppData\Local\Programs\Python\Python313

Image20 (1)

  1. After entering the path, click OK to save the changes.
  2. To verify, open the command prompt and type python. If the path to the interpreter is correctly specified, the Python console will open.

Image33 (1)

Setting Up Python Interpreter in Visual Studio Code

In Windows

Once Python is installed, you need to connect it to Visual Studio Code. To do this:

  1. Open Visual Studio Code and click the New File... button on the home page to create a new file. 

Image12 (1)

Alternatively, you can create a Python project in Visual Studio Code by clicking on Open Folder…, where you can select the entire project folder containing the files.

  1. Type any name for the file, use the .py extension, and press Enter.

Image21

  1. Save the file in any location. Ensure that the file name ends with the .py extension.

Image23 (1)

  1. Once the file is saved, the interface in Visual Studio Code will display a prompt at the bottom right, suggesting you install the recommended Python extension.

Image1

  1. To run Python in Visual Studio Code, you first need to select the Python interpreter. A button will appear at the bottom of the panel with a warning: Select Interpreter. Click on it.

Image11 (1)

  1. In the menu that appears, select Enter interpreter path… and press Enter.

Image24

  1. Specify the full path to the Python interpreter. By default, it is located at:
C:\Users\Administrator\AppData\Local\Programs\Python\Python313

where Administrator is your user account name. Select the file named python and click on Select Interpreter.

Image17

To test it, write a simple program that calculates the square root of a number:

import math

num = float(input("Enter a number to find its square root: "))

if num >= 0:
    sqrt_result = math.sqrt(num)
    print(f"The square root of {num} is {sqrt_result}")
else:
    print("Square root of a negative number is not real.")

In Visual Studio Code, to run the Python code, click the Run Python File button at the top-right. If the interpreter is set up correctly, the program will run successfully.

Image8

In macOS

On macOS operating systems, the Python interpreter is automatically recognized. Simply create a new .py file as described in the Windows section above and run the program directly.

Image4

In Ubuntu

Similarly to macOS, in the Ubuntu distribution, VS Code automatically detects the installed Python interpreter in the system. All you need to do is create a new .py file and run the program directly.

Image18 (1)

Recommended Extensions for Python in Visual Studio Code

VS Code offers a wide range of Python extensions (plugins) that simplify the development process. Here are some of the most popular ones:

Pylance

Pylance provides code analysis, autocompletion, and IntelliSense support, making Python development more efficient and user-friendly. Key features include fast autocompletion, type checking, and IntelliSense support.

Image9

Jupyter

The Jupyter extension is a powerful tool for working with interactive notebooks directly in the editor.  It’s especially useful for data analysis, machine learning, visualization, and interactive programming tasks.

Image28

autoDocstring — Python Docstring Generator

autoDocstring is a popular extension that helps automatically generate docstrings for Python functions, methods, and classes. Docstrings improve code readability and serve as built-in documentation.

Image25

isort

isort is a tool for automatically sorting and organizing imports in Python code. You can configure it in Visual Studio Code to make working with imports easier and to improve code readability.

Image10

Conclusion

This article covered the installation and setup of Visual Studio Code for Python development. Visual Studio Code offers full support for Python and provides the ability to extend its functionality through various plugins, making the coding process easier and more efficient.

Python
05.02.2025
Reading time: 10 min

Similar

Python

Functions in Python

Functions in Python are blocks of reusable code that you can access by calling the function name and passing arguments. Using functions in Python significantly simplifies a programmer's work because, instead of writing code repeatedly, one can simply call a function. How to Create a Function in Pyhton Let's start with an example and then move on to the explanation: def multiply(first, second):    return first * second We have just written a function that performs a simple task: it multiplies the values (arguments) passed to it. These values can then be entered after the function name in the program to get the product of the factors. Now, enter the following in IDLE: >>> multiply(7, 8) Arguments can include not only whole numbers but also decimal numbers, for example: >>> multiply(7.4, 8.2)60.68 Now, let's break down the code. Here, we define a Python function using the def keyword and the function name. In parentheses, we specify parameters that will accept various arguments from user input. A colon must follow the closing parenthesis, after which a new line with indentation starts the function body, describing what the function does. If you're writing code in an editor, the indentation will be added automatically. We used the return operator, which explicitly returns arguments. Note that after return, there is an instruction on what the program should do with the arguments. In this case, it multiplies them. Practical Example of Using Python Functions Here, we will demonstrate how Python functions help optimize routine tasks. The following example is simplified but illustrative. By understanding how functions work, you can learn to solve your own tasks, which will become more complex and interesting as you progress in the language. Let's say we opened a bookstore and purchased a cash register, and the cashier had already issued receipts for the first customers. Initially, a receipt might look like this: print("Learn Now, LLC") print("Programming Book", end=" ") print(1, end=" pcs. ") print(50, end=" euro") print("\nAdvanced Programming Book", end=" ") print(1, end=" pcs. ") print(100, end=" euro") print("\nTotal:", 150, end=" euro") print("\nThank you for your purchase!") Output: Learn Now, LLC Programming Book 1 pcs. 50 euro Advanced Programming Book 1 pcs. 100 euro Total: 150 euro Thank you for your purchase! Now, imagine that a whole stack of books has been purchased, and the number of customers is increasing daily. While you manually calculate the total for one customer, others start getting impatient. This is where automation comes in.  Let's say someone buys seven different books, with some books purchased in multiple copies: def check(book_attr): total = 0 print("Learn Now, LLC") for book in book_attr: a = book[0] b = book[1] c = book[2] print(f"{a} ({b} pcs.) - {c} euro") total += b * c print(f"\nTotal: {total} euro") print("Thank you for your purchase!") book_attr = [ ("Programming Book", 2, 50), ("Advanced Programming Book", 2, 100), ("Programming Book 80 lvl", 2, 195), ("Beginner's Guide to Python", 1, 120), ("You Can Become a Programmer", 1, 98), ("Functional Programming in Python", 1, 95), ("Secrets of Clean Code", 1, 80), ] As we can see, new variables appeared, and the purchase list was placed in a separate block. Now, when generating a new receipt, all we need to do for automatic total calculation is enter the book names, quantities, and prices per unit. Once all items are entered, we call our function with the parameter formatted as a tuple above: check(book_attr) This produces the following output: Learn Now, LLC Programming Book (2 pcs.) - 50 euro Advanced Programming Book (2 pcs.) - 100 euro Programming Book 80 lvl (2 pcs.) - 195 euro Beginner's Guide to Python (1 pcs.) - 120 euro You Can Become a Programmer (1 pcs.) - 98 euro Functional Programming in Python (1 pcs.) - 95 euro Secrets of Clean Code (1 pcs.) - 80 euro Total: 1083 euro Thank you for your purchase! That's it! The total amount was calculated automatically. Let’s break down the code: The variable total stores the purchase total and changes as new values are added to the tuple. A for loop is used to define a set of variables that store the following values: a: product name b: quantity c: price per unit Next, we give the print command. The letter f in print statements (which is itself a built-in function, by the way) means that f-strings are used. For now, it's enough to know that they are a convenient formatting method, and the code is self-explanatory. The next line should not be surprising: it calculates the total by multiplying the quantity of each item by its price and adding the result to the running total. Finally, we use another f-string for text formatting, and we have already discussed the tuple block that stores the necessary data for purchase calculations. Features of Functions in Python Key Advantages: No need to repeat specific blocks of code, which can sometimes be quite large. Functions can be called as many times as needed, even consecutively. When divided into multiple functional blocks, large programs become much easier to track. There are almost no downsides to functions in Python, except that they may not always be convenient. In some cases, it is easier to use generators, as certain functions (e.g., filter) may return iterators, requiring additional code to process them. For example, if we enter the following in IDLE: >>> numbers = [2, 4, 6, 8, 10, 12, 14] >>> filter(lambda num: num >= 10, numbers) We get this result: <filter object at 0x00000000030C3220> To correctly display elements that meet the condition, we need to wrap this expression as follows: >>> list(filter(lambda num: num >= 10, numbers))[10, 12, 14] Built-in Functions in Python You have almost certainly used them in your first Python lesson. Here’s an example: print("Hello, World!") The print function is a built-in function, and "Hello, World!" is its argument. Python has hundreds, even thousands, of built-in functions, especially when additional libraries are included. You don't need to know all of them; you can always check the documentation if you encounter an unfamiliar function. However, you will need to learn some common built-in functions, as these core elements are essential for writing any useful program. Here are some commonly used built-in functions: len returns the length (number of elements) of a sequence such as a string, list, tuple, range, or array: flowers = ["bellflower", "cornflower", "buttercup", "forget-me-not", "daisy"]len(flowers) Output: 5 str converts numbers into strings (since Python does not allow direct concatenation of strings and numbers): year = 2008"Euro " + str(year) Output: 'Euro 2008' int converts strings into integers. It also rounds floating-point numbers to the nearest integer, always towards zero: int(554.995) Output: 554 float converts integer values into floating-point numbers, which can be useful for certain calculations: float(55) Output: 55.0 tuple converts lists into tuples: flowers = ["bellflower", "cornflower", "buttercup", "forget-me-not", "daisy"]tuple(flowers) Output: ('bellflower', 'cornflower', 'buttercup', 'forget-me-not', 'daisy') dict allows you to create dictionaries. Here’s an example of creating a dictionary from a list of tuples using dict: clubs = [('Barcelona', 1), ('Juventus', 3), ('Liverpool', 2), ('Real Madrid', 5), ('Bayern München', 4)]dict(clubs) Output: {'Barcelona': 1, 'Juventus': 3, 'Liverpool': 2, 'Real Madrid': 5, 'Bayern München': 4} range creates number sequences, which can be useful for iterating through numeric values: for number in range(0, 30, 3): print(number) Output: 0 3 6 9 12 15 18 21 24 27 The range function takes three parameters: The first two define the range limits. The third (optional) parameter specifies the step. In this case, numbers from 0 to 30 are printed in steps of 3. The upper bound is not included in the output. To include it, the range should be extended slightly: for number in range(0, 31, 3): print(number) Output: 0 3 … 27 30 Using the Result of One Function in Another Python Function Finally, let’s look at another interesting technique. Since functions in Python are objects, they can be passed as arguments to other functions and referenced. def check(company="Learn Now"): """Allows inserting different company names in the receipt""" print(f"{company}, LLC") Let’s enter the name of another company: check("Enlightenment") Output: Enlightenment, LLC Now, let’s pass the created function to the built-in help function to learn what it does: help(check) Output: Help on function check in module __main__: check(company='Learn Now') Allows inserting different company names in the receipt As we can see, it is quite simple. What We Learned In this tutorial, we explored how functions work in Python 3 and learned how to create and use them. We discussed built-in tools and examined an example of passing functions as objects to other functions. By studying functions more deeply, you will appreciate their usefulness even when writing relatively small applications.
02 April 2025 · 8 min to read
Python

Comments in Python 3

Comments in a program are parts of the code that are ignored by the interpreter or compiler. They are used to: Make the code more readable; Explain what the code does and why; Prevent parts of the code from executing during testing/execution; Leave notes about things that need to be done/modified/removed. Overall, comments are meant to make a programmer's life easier—they play no role for the computer. However, in some programming methodologies, such as extreme programming, it is believed that if code needs comments, then the code is poorly written. In this article, you will learn how to write comments in Python 3 and what Docstrings and PEP are. Comments in Python Different programming languages use different syntax for comments. Often, it's a double slash (//). In Python 3, comments in the code start with the # symbol. For example: # The code prints "Hello, World!" to the consoleprint("Hello, World!") You can also place a comment on the same line as the code: print("Hello, World!")  # The code prints "Hello, World!" to the console Comments should be useful to the reader. For example, this comment is not helpful: # This code clearly does somethingprint("Hello, World!") A good comment should explain or describe the code and its purpose. Some developers believe that comments should describe the programmer’s intent. In general, it is best to think of comments as a form of code documentation. If they are not useful, they should be removed. You can also use comments to disable parts of the code to prevent them from executing. This can be useful for testing and debugging. Suppose we need to comment the following code: db_lp = sqlite3.connect('login_password.db') cursor_db = db_lp.cursor() sql_create = '''CREATE TABLE passwords( login TEXT PRIMARY KEY, password TEXT NOT NULL);''' cursor_db.execute(sql_create) db_lp.commit() cursor_db.close() db_lp.close() The goal of commenting is to make this block of code understandable. For example, we can comment it like this: db_lp = sqlite3.connect('login_password.db') # Creating the login_password database cursor_db = db_lp.cursor() # Cursor object for executing SQL queries # SQL query to create the "passwords" table in the database sql_create = '''CREATE TABLE passwords( login TEXT PRIMARY KEY, password TEXT NOT NULL);''' cursor_db.execute(sql_create) # Executing the sql_create query db_lp.commit() # Committing changes # Closing the Cursor and database cursor_db.close() db_lp.close() Manually commenting Python code can be inconvenient. To format a block of code as single-line comments, you can use keyboard shortcuts: PyCharm: Ctrl + / Visual Studio Code: To comment/uncomment a line Ctrl + /, for a block of code Shift + Alt + A Eclipse: To comment/uncomment a line Ctrl + /, for a block of code Ctrl + Shift + / Visual Studio: Ctrl + K then Ctrl + C to comment a block of code, and Ctrl + K then Ctrl + U to uncomment it Docstring in Python A Docstring is a string literal placed immediately after the declaration of a module, function, class, or other structure. It is a convenient way to document code, making it accessible for reference. Docstrings were introduced in Python in 2001 and are described in PEP 257. What is PEP? Python's development follows a structured process involving creating, discussing, selecting, and implementing PEP (Python Enhancement Proposal) documents. PEPs contain proposals for language development, including new features, modifications to existing ones, etc. One of the most well-known and useful PEP documents is PEP 8, which outlines guidelines and conventions for writing Python code. If you plan to write in Python, familiarize yourself with these conventions. Since there are many rules, special tools exist to help enforce them. Some useful tools are listed below. Now, back to Docstring. A Docstring is the first statement in an object's declaration. Here’s an example: def function(x, y, z): """ Docstring of this function """ def inner_function(): """ Docstring of the nested function """ The syntax for a Docstring is three double quotes at the beginning and end. You can also use single quotes or fewer than three quotes, but PEP 257 recommends using three double quotes. You can access an object’s Docstring using the __doc__ method: def subtraction(a, b): """Function subtracts b from a""" return a - b print(subtraction.__doc__) Output: Function subtracts b from a You can also use the __doc__ property to get information about built-in Python methods, such as print: print(print.__doc__) Output: print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. String literals placed anywhere in the Python code can also serve as documentation. The Python bytecode compiler will not recognize them, and they will not be accessible at runtime via __doc__. However, there are two additional types of Docstrings that can be extracted using documentation tools. Additional Docstrings Additional Docstrings are string literals ignored by the Python compiler but recognized by Docutils tools. They are placed immediately after a Docstring. Example: def function(arg): """This is the Docstring of this function. It will be available via __doc__.""" """ This is an additional Docstring. It will be ignored by the compiler but recognized by Docutils. """ pass Attribute Docstrings Attribute Docstrings are string literals placed immediately after a simple assignment at the module, class, or __init__ method level. Example: def f(x): """This is the Docstring of this function. It will be available via __doc__""" return x**2 f.a = 1 """ This is an Attribute Docstring for the attribute f.a, it will be ignored by the compiler but recognized by Docutils. """ Here are the main PEP 257 guidelines for using docstrings: Leave a blank line after all Docstrings. The script's Docstring should serve as a "usage message," potentially displayed to the user if incorrect arguments are provided. It should describe functionality, parameter syntax, environment variables, and files used. The module's Docstring should list important objects with a one-line explanation for each. The function/method Docstring should describe behavior, arguments, return values, possible exceptions, and constraints. The class Docstring should include methods, instance variables, and describe the class behavior. The constructor (__init__) should have its own separate Docstring. If a class is a subclass and mostly inherits behavior from a parent class, its documentation should mention this and describe any differences. Useful Tools Here are some tools to help with PEP 8 and comments in Python 3: pycodestyle — Checks if your code follows PEP 8. Black — Formats code according to PEP 8 (mostly). Doxygen, PyDoc, pdoc — Automatically generate documentation from Docstrings.
02 April 2025 · 6 min to read
Python

How to Install and Set Up PyTorch

PyTorch is a free, open-source deep learning library. With its help, a computer can detect objects, classify images, generate text, and perform other complex tasks. PyTorch is also a rich tool ecosystem that supports and accelerates AI development and research. In this article, we will cover only the basics: we will learn how to install PyTorch and verify that it works. To work with PyTorch, you will need: At least 1 GB of RAM. Installed Python 3 and pip.  A configured local development environment. Deep knowledge of machine learning is not required for this tutorial. It is assumed that you are familiar with basic Python terms and concepts. Installing PyTorch We will be working in a Windows environment but using the command line. This makes the tutorial almost universal—you can use the same commands on Linux and macOS. First, create a workspace where you will work with Torch Python. Navigate to the directory where you want to place the new folder and create it: mkdir pytorch Inside the pytorch directory, create a new virtual environment. This is necessary to isolate projects and, if needed, use different library versions. python3 -m venv virtualpytorch To activate the virtual environment, first go to the newly created directory: cd virtualpytorch Inside, there is a scripts folder (on Windows) or bin (on other OS). Navigate to it: cd scripts Activate the virtual environment using a bat file by running the following command in the terminal: activate.bat The workspace is now ready. The next step is to install the PyTorch library. The easiest way to find the installation command is to check the official website. There is a convenient form where you select the required parameters. As an example, install the stable version for Windows using CPU via pip. Select these parameters in the form, and you will get the necessary command: pip3 install torch torchvision torchaudio Copy and execute the pip install torch command in the Windows command line. You are also installing two sub-libraries: torchvision – contains popular datasets, model architectures, and image transformations for computer vision. torchaudio – a library for processing audio and signals using PyTorch, providing input/output functions, signal processing, datasets, model implementations, and application components. This is the standard setup often used when first exploring the library. The method described above is not the only way to install PyTorch. If Anaconda is installed on Windows, you can use its graphical interface. If your computer has NVIDIA GPUs, you can select the CUDA version instead of CPU. In that case, the installation command will be different. All possible local installation methods are listed in the official documentation. You can also find commands for installing older versions of the library there. To install them, just select the required version and install it the same way as the current package builds. You don't need to write a script to check if the library is working. The Python interpreter has enough capabilities to perform basic operations. If you have successfully installed PyTorch in the previous steps, then launching the Python interpreter won’t be an issue. Run the following command in the command line: python Then enter the following code: import torch x = torch.rand(5, 3) print(x) You should see an output similar to this: tensor([[0.0925, 0.3696, 0.4949], [0.0240, 0.2642, 0.1545], [0.7274, 0.4975, 0.0753], [0.4438, 0.9685, 0.5022], [0.4757, 0.6715, 0.4298]]) Now, you can move on to solving more complex tasks. PyTorch Usage Example To make learning basic concepts more engaging, let’s do it in practice. For example, let’s create a neural network using PyTorch that can recognize the digit shown in an image. Prerequisites To create a neural network, we need to import eight modules: import torch import torchvision import torch.nn.functional as F import matplotlib.pyplot as plt import torch.nn as nn import torch.optim as optim from torchvision import transforms, datasets All of these are standard PyTorch libraries plus Matplotlib. They handle image processing, optimization, neural network construction, and graph visualization. Loading and Transforming Data We will train the neural network on the MNIST dataset, which contains 70,000 images of handwritten digits. 60,000 images will be used for training. 10,000 images will be used for testing. Each image is 28 × 28 pixels. Each image has a label representing the digit (e.g., 1, 2, 5, etc.). train = datasets.MNIST("", train=True, download=True, transform=transforms.Compose([transforms.ToTensor()])) test = datasets.MNIST("", train=False, download=True, transform=transforms.Compose([transforms.ToTensor()])) trainset = torch.utils.data.DataLoader(train, batch_size=15, shuffle=True) testset = torch.utils.data.DataLoader(test, batch_size=15, shuffle=True) First, we divide the data into training and testing sets by setting train=True/False. The test set must contain data that the machine has not seen before. Otherwise, the neural network’s performance would be biased. Setting shuffle=True helps reduce bias and overfitting. Imagine that the dataset contains many consecutive "1"s. If the machine gets too good at recognizing only the digit 1, it might struggle to recognize other numbers. Shuffling the data prevents the model from overfitting specific patterns and ensures a more generalized learning process. Definition and Initialization of the Neural Network The next step is defining the neural network: class NeuralNetwork(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(784, 86) self.fc2 = nn.Linear(86, 86) self.fc3 = nn.Linear(86, 86) self.fc4 = nn.Linear(86, 10) def forward(self, x): x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.relu(self.fc3(x)) x = self.fc4(x) return F.log_softmax(x, dim=1) model = NeuralNetwork() The neural network consists of four layers: one input layer, two hidden layers, and one output layer. The Linear type represents a simple neural network. For each layer, it is necessary to specify the number of inputs and outputs. The output number of one layer becomes the input for the next layer. The input layer has 784 nodes. This is the result of multiplying 28 × 28 (the image size in pixels). The first hidden layer has 86 output nodes, so the input to the next layer must be 86 as well.The same logic applies further. 86 is an arbitrary number—you can use a different value. The output layer contains 10 nodes because the images represent digits from 0 to 9. Each time data passes through a layer, it is processed by an activation function. There are several activation functions. In this example, we use ReLU (Rectified Linear Unit). This function returns 0 if the value is negative or the value itself if it is positive. The softmax function is used at the output layer to normalize values. For example, it might return an 80% probability that the digit in the image is 1, or a 30% probability that the digit is 5, and so on. The highest probability is selected as the final prediction. Training The next step is training. optimizer = optim.Adam(model.parameters(), lr=0.001) EPOCHS = 3 for epoch in range(EPOCHS): for data in trainset: X, y = data model.zero_grad() output = model(X.view(-1, 28 * 28)) loss = F.nll_loss(output, y) loss.backward() optimizer.step() print(loss) The optimizer calculates the difference (loss) between the actual data and the prediction, adjusts the weights, recalculates the loss, and continues the cycle until the loss is minimized. Training Verification Here, we compare the actual values with the predictions made by the model. For this tutorial, the accuracy is high because the neural network effectively recognizes each digit. correct = 0 total = 0 with torch.no_grad(): for data in testset: data_input, target = data output = model(data_input.view(-1, 784)) for idx, i in enumerate(output): if torch.argmax(i) == target[idx]: correct += 1 total += 1 print('Accuracy: %d %%' % (100 * correct / total)) To verify that the neural network works, pass it an image of a digit from the test set: plt.imshow(X[1].view(28,28)) plt.show() print(torch.argmax(model(X[1].view(-1, 784))[0])) The output should display the digit shown in the provided image. Final Script Here’s the full script you can run to see how the neural network works: import torch import torchvision import torch.nn.functional as F import matplotlib.pyplot as plt import torch.nn as nn import torch.optim as optim from torchvision import transforms, datasets train = datasets.MNIST("", train=True, download=True, transform = transforms.Compose([transforms.ToTensor()])) test = datasets.MNIST("", train=False, download=True, transform = transforms.Compose([transforms.ToTensor()])) trainset = torch.utils.data.DataLoader(train, batch_size=15, shuffle=True) testset = torch.utils.data.DataLoader(test, batch_size=15, shuffle=True) class NeuralNetwork(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(784, 86) self.fc2 = nn.Linear(86, 86) self.fc3 = nn.Linear(86, 86) self.fc4 = nn.Linear(86, 10) def forward(self, x): x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.relu(self.fc3(x)) x = self.fc4(x) return F.log_softmax(x, dim=1) model = NeuralNetwork() optimizer = optim.Adam(model.parameters(), lr=0.001) EPOCHS = 3 for epoch in range(EPOCHS): for data in trainset: X, y = data model.zero_grad() output = model(X.view(-1, 28 * 28)) loss = F.nll_loss(output, y) loss.backward() optimizer.step() print(loss) correct = 0 total = 0 with torch.no_grad(): for data in testset: data_input, target = data output = model(data_input.view(-1, 784)) for idx, i in enumerate(output): if torch.argmax(i) == target[idx]: correct += 1 total += 1 print('Accuracy: %d %%' % (100 * correct / total)) plt.imshow(X[1].view(28,28)) plt.show() print(torch.argmax(model(X[1].view(-1, 784))[0])) Each time we run the network, it will take a random image from the test set and analyze the digit depicted on it. After the process is completed, it will display the recognition accuracy in percentage, the image itself, and the digit recognized by the neural network. This is how it looks: Conclusion PyTorch is a powerful open-source machine learning platform that accelerates the transition from research prototypes to production deployments. With it, you can solve various tasks in the fields of artificial intelligence and neural networks. You don’t need deep knowledge of machine learning to begin working with PyTorch. It is enough to know the basic concepts to repeat and even modify popular procedures like image recognition to suit your needs. A big advantage of PyTorch is the large user community that writes tutorials and shares examples of using the library. Object recognition in images is one of the simplest and most popular tasks in PyTorch for beginners. However, the capabilities of the library are not limited to this. To create powerful neural networks, you need a lot of training data. These can be stored, for example, in an object-based S3 storage such as Hostman, with instant data access via API or web interface. This is an excellent solution for storing large volumes of information.
01 April 2025 · 10 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support