VS Code for Python – A complete Guide to Install and Setup VS Code

Set Up VS Code For Python

In this tutorial, we are going to discuss how to set up VS Code for Python programming.


What is VS Code?

VS Code stands for Visual Studio Code. It is a lightweight, yet powerful source code editing software developed by Microsoft. Visual Studio Code has been developed as a desktop application. And it is available for famous operating systems like macOS, Microsoft Windows, and Linux. It comes with built-in support for the frameworks like Node.js, TypeScript, and JavaScript.

It also has a very large ecosystem of extensions for supporting several other frameworks and programming languages like C, C++, Python, etc. These extensions are the most important feature of the VS Code which redefines the experience of code editing takes it to the next level.

Let’s discuss how we can configure our Visual Studio Code for Python programming.

1. Download VS Code

As the very first step, we have to download the latest version of Visual Studio Code from its official website code.visualstudio.com.

Download VS Code
Download VS Code

Steps to install VS Code for Python

After the download of the VS Code installer file completes, follow the steps given below to install the software on your local (Windows) machine.

  1. Double click on the installer file.
  2. Select the destination folder where you want to install VS Code.
  3. You can optionally create a start menu folder where the setup will create the program’s shortcuts.
  4. After these you can select some additional taskes which you want the VS Code setup to perform like:
    • Create a desktop icon
    • Add “Open with Code” action to Windows Explorer (file context menu)
    • Add “Open with Code” action to Windows Explorer (directory or folder context menu)
    • Register VS Code as an editor for the supported file types
    • Add to path which requires shell restart
  5. Click Install to begin the installation process. Once done, click Finish.

2. Install the Python Interpreter for VS Code

Now, we have to download the latest version of Python Interpreter from its official website www.python.org.

Download Python Interpreter
Download Python Interpreter

Steps to install Python Interpreter

After you download the Python installer file, follow the steps given below to install the interpreter on your local (Windows) machine.

  • Double click the installer file.
  • Check the option “Add Python to PATH”.
  • Click on one of the two options shown on the screen
    • Install Now option will install the Python Interpreter with the default settings (recommended for beginners)
    • Customize Installation option will allow us to enable or disable the features
  • Finally, click Close which appears after the installation process completes.

We can also verify if the Python Interpreter has been successfully installed by running the following commands on the terminal:

  • For Windows, run the following command on the Command Prompt or the PowerShell.
C:\Users\Guest> py -3 --version
  • For Linux or macOS machine run the following command on the terminal.
ubuntu:~$ python3 --version

The outputs in the case of both the commands will show the version of the Python installed on the system only if the Python is successfully installed on the system.

3. Install the Python extension for VS Code

After installing the VS Code Software as well as the Python Interpreter, we have to install the Python extension for VS Code. Python extension is a Visual Studio Code extension developed by Microsoft that has numerous supporting features such as IntelliSense, Code navigation, Code formating, linting, debugging, etc. for all the supported versions of Python language (>=3.6).

Python Extension
Python extension

Steps to install the Python extension

To install the Python extension for VS Code follow the steps given below:

  • Open the VS Code Extension Marketplace using the shortcut (Ctrl+Shift+X).
  • Search for the Python extension by typing “Python” in the Extension Marketplace search bar.
  • Select the first option with name Python from the search result which is star marked.
  • Then click on the Install button.

NOTE: When we install the Python extension for VS Code, the Pylance and Jupyter extensions automatically get installed to provide us the best possible experience while working with the Python (.py) files and Jupyter (.ipynb) notebooks. The Pylance and Jupyter extensions are optional dependencies even if we uninstall these extensions the main Python extension will remain fully functional.

Useful features of Python extension

  • IntelliSense (Pylance)
  • Refactoring
  • Code Formating
  • Linting
  • Debugging
  • Testing
  • Environments
  • Jupyter Notebook (Jupyter)

4. Different modes in VS Code to work with Python

In Visual Studio Code, we have three different modes to work with Python language. Let’s discuss them one by one.

Python Script File

In this mode, we can either create a new Python file or open an existing Python file with .py extension in VS Code. But we can only run the Python script file as a whole on the VS Code integrated terminal.

Python Script File
Python Script File

Python Interactive Mode

In this mode, we create a normal Python script file then we have to activate the Python Interactive Mode in it by typing [ # %% ]. This will create one code cell for us where can we execute the Python instructions distinctly either by clicking Run Cell or by using the shortcut (Shift+Enter). Once we run a code cell an interactive window will open on the side of the Python script file. We can also debug our Python instructions within the individual code cell by pressing the Debug Cell.

Python Interactive Mode
Python Interactive Mode

Interactive Python Notebook or Jupyter Notebook

In this mode, we can either create a new Python Jupyter Notebook or open an existing Python Jupyter Notebook with .ipynb extension in VS Code. Here we can only run the Python instructions within an already presented code cell. We cannot run all the Python instructions in the Jupyter Notebook as a whole on the VS Code integrated terminal. To run the individual code cell either press the Run Cell button above each code cell or by using the shortcut (Shift+Enter).

Python Interactive Notebook
Python Interactive Notebook

Conclusion

In this tutorial, we have learned what is VS Code for Python, how to download and install it on the local windows machine. We have also learned about various VS Code extensions like Python, Pylance, and Jupyter which have rich support for the Python language. We too learn to work with different Python modes in VS Code like Python script file, Python interactive mode, and Python Jupyter Notebook. Hope you have set up your Visual Studio Code software and ready to code in Python language on it.