Javatpoint Logo
Javatpoint Logo

How to set up Python in Visual Studio Code

The Microsoft Python plugin makes working with Python in Visual Studio Code easy, enjoyable, and productive. As a result of the addition, VS Code functions flawlessly as a Python editor across all operating systems and Python interpreters. It uses all of VS Code's capabilities to offer linting, debugging, auto-completion, IntelliSense, unit testing, and quickly switch between virtual and Conda environments for Python.

This page only provides a summary of the various features of the Python extension for VS Code. Use the button below to access a tutorial on editing, running, and debugging code.

Install Python and Extension for Python.

You can follow the tutorial's instructions to install Python and use the extension. A Python interpreter must be installed separately from the extension. For a quick installation, use Python from and the extension from the VS Code Marketplace.

Use the Python: Select Interpreter command to activate Python after installation. Consult Environments - Manually select an interpreter if VS Code cannot find the one you're looking for.

Through options, you may customize the Python addon. The Python Settings reference has more information.

If you use Windows, WSL is an excellent tool for Python development. Windows Subsystem for Linux.

Execute Python Code

To acquire a feel for Python, create a file called and paste the code below into it:

The following shortcuts are available through the Python extension to run Python code in the presently chosen interpreter (Python: Select Interpreter in the Command Palette):

Run the Python file in the terminal by performing a right-click anywhere in the text editor. Only the selected option is run when it is active.

Run Python File in Terminal by doing a right-click on a Python file in Explorer.

The Terminal: Create New Terminal command can also be used to create a terminal in which Visual Studio Code immediately launches the currently selected interpreter. Look below at Environments. The Python: Start REPL command launches the Python REPL after activating a terminal with the presently chosen interpreter.

IntelliSense and Autocomplete

Using the configured interpreter, the Python addon enables code completion and IntelliSense. Multiple capabilities are referred to together as "intelligent code completion" (in-context method and variable suggestions) throughout all of your files, as well as for built-in and third-party modules.

As you type, IntelliSense immediately shows methods, class members, and documentation. You may also use Ctrl+Space to force completions at any moment. You can also hover your cursor over identifiers to learn more about them.

  • Linting

Linting checks your Python code for potential flaws, making it simple to find and fix various issues.

Various linters, including Pylint, pycodestyle, Flake8, mypy, pygostyle, prospector, and pylama, can be applied using the Python extension.

  • Debugging

Print statement debugging is finished! Set breakpoints, look at data, and use the debug console while you run your programme repeatedly.

Debugging print statements is over! As you run your program iteratively, set breakpoints, examine data, and use the debug console. Debug many Pythons application types, such as multi-threaded, web, and remote apps.

See Debugging for information particular to Python, such as setting up your launch. Json setup and remote debugging. The debugging document contains general information on VS Code debugging. The lessons for Django and Flask also show how to debug those web applications, including how to debug Django page templates.

  • Environments

The Python plugin finds installed Python interpreters in common places automatically. Additionally, it finds virtual and Conda environments in the workspace folder. Observe Python environment configuration.

The current environment is shown in the VS Code Status Bar on the left:

How to set up Python in Visual Studio Code

If an interpreter is not selected, the Status Bar also shows:

How to set up Python in Visual Studio Code

In addition to debugging, the chosen environment is used for language-related capabilities like IntelliSense, auto-completions, linting, and formatting. Additionally, it becomes active when Python is launched in a terminal.

Use Python: Select the Interpreter command or click on the name of the current interpreter in the Status Bar to change it, including to conda or virtual environments.

How to set up Python in Visual Studio Code

You are given a list of recognized environments by VS Code, along with those you have manually added to your user settings.

  • Setting up Packages

The Terminal panel and commands like pip install package name> (Windows), and pip3 install package name> (macOS/Linux) are used to install packages. That package and all its dependencies are installed into your project by VS Code. Examples are provided in the Django and Flask tutorials and the Python instruction.

  • Jupyter Notebook

If you open a Jupyter notebook file (.ipynb) in Visual Studio Code, the Jupyter Notebook Editor will allow you to immediately analyse, modify, and execute code cells.

How to set up Python in Visual Studio Code

The notebook can be converted into and opened as a Python code file. The notebook's cells are separated in the file by #%% comments, and the Python extension for the file shows Run Cell or Run All Cells CodeLens. By choosing one of the two CodeLens options, the Python interactive window's cell(s) and the Jupyter server are both launched:

How to set up Python in Visual Studio Code

You may utilize VS Code's debugging features by opening a notebook as a Python file. The created notebook file can then be uploaded to a service like Azure Notebooks or stored and used as a notebook in Jupyter, the Notebook Editor.

In order to execute the code, you can also connect to a remote Jupyter server using either the Notebook Editor or a Python file.. Visit Jupyter support for further details.

  • Testing

Testing using unit tests and pytest is supported by the Python extension.

You must enable one of the frameworks in settings to run tests. Each framework provides options, like arguments that specify test discovery paths and patterns.

When used properly, VS Code offers several commands (in the Status Bar, the Command Palette, and elsewhere) for running and debugging tests, including the capability to execute specific test files and methods.

  • Configuration

The Python addon offers a huge selection of settings for each of its different capabilities. They discuss their respective themes: testing, debugging, linting, and editing code.

  • Other well-known Python Addons

All the capabilities outlined in the earlier sections of this article are available in the Microsoft Python extension. Installing additional well-liked Python extensions will increase VS Code's support for the Python language.

  • To see the Extensions view, use Ctrl+Shift+X.
  • To narrow the list of extensions, type "python".

Next TopicHow to use PyCharm

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA