Python in Visual Studio Code (2023)

Working with Python in Visual Studio Code, using the Microsoft Python extension, is simple, fun, and productive. The extension makes VS Code an excellent Python editor, and works on any operating system with a variety of Python interpreters. It leverages all of VS Code's power to provide auto complete and IntelliSense, linting, debugging, and unit testing, along with the ability to easily switch between Python environments, including virtual and conda environments.

This article provides only an overview of the different capabilities of the Python extension for VS Code. For a walkthrough of editing, running, and debugging code, use the button below.

Python Hello World Tutorial

Install Python and the Python extension

The tutorial guides you through installing Python and using the extension. You must install a Python interpreter yourself separately from the extension. For a quick install, use Python from python.org and install the extension from the VS Code Marketplace.

Once you have a version of Python installed, activate it using the Python: Select Interpreter command. If VS Code doesn't automatically locate the interpreter you're looking for, refer to Environments - Manually specify an interpreter.

You can configure the Python extension through settings. Learn more in the Python Settings reference.

Windows Subsystem for Linux: If you are on Windows, WSL is a great way to do Python development. You can run Linux distributions on Windows and Python is often already installed. When coupled with the WSL extension, you get full VS Code editing and debugging support while running in the context of WSL. To learn more, go to Developing in WSL or try the Working in WSL tutorial.

(Video) Getting Started with Python in Visual Studio Code | Python with VSCode

Run Python code

To experience Python, create a file (using the File Explorer) named hello.py and paste in the following code:

print("Hello World")

The Python extension then provides shortcuts to run Python code in the currently selected interpreter (Python: Select Interpreter in the Command Palette):

  • In the text editor: right-click anywhere in the editor and select Run Python File in Terminal. If invoked on a selection, only that selection is run.
  • In Explorer: right-click a Python file and select Run Python File in Terminal.

You can also use the Terminal: Create New Terminal command to create a terminal in which VS Code automatically activates the currently selected interpreter. See Environments below. The Python: Start REPL activates a terminal with the currently selected interpreter and then runs the Python REPL.

For a more specific walkthrough on running code, see the tutorial.

Autocomplete and IntelliSense

The Python extension supports code completion and IntelliSense using the currently selected interpreter. IntelliSense is a general term for a number of features, including intelligent code completion (in-context method and variable suggestions) across all your files and for built-in and third-party modules.

IntelliSense quickly shows methods, class members, and documentation as you type, and you can trigger completions at any time with ⌃Space (Windows, Linux Ctrl+Space). You can also hover over identifiers for more information about them.

Python in Visual Studio Code (1)

Tip: Check out the IntelliCode extension for VS Code (preview). IntelliCode provides a set of AI-assisted capabilities for IntelliSense in Python, such as inferring the most relevant auto-completions based on the current code context.

Enhance completions with AI

GitHub Copilot is an AI-powered code completion tool that helps you write code faster and smarter. You can use the GitHub Copilot extension in VS Code to generate code, or to learn from the code it generates.

(Video) How to set up Python on Visual Studio Code in 2021

Python in Visual Studio Code (2)

GitHub Copilot provides suggestions for numerous languages and a wide variety of frameworks, and it works especially well for Python, JavaScript, TypeScript, Ruby, Go, C# and C++.

You can learn more about how to get started with Copilot in the Copilot documentation.

Linting

Linting analyzes your Python code for potential errors, making it easy to navigate to and correct different problems.

The Python extension can apply a number of different linters including Pylint, pycodestyle, Flake8, mypy, pydocstyle, prospector, and pylama. See Linting.

Debugging

No more print statement debugging! Set breakpoints, inspect data, and use the debug console as you run your program step by step. Debug a number of different types of Python applications, including multi-threaded, web, and remote applications.

For Python-specific details, including setting up your launch.json configuration and remote debugging, see Debugging. General VS Code debugging information is found in the debugging document. The Django and Flask tutorials also demonstrate debugging in the context of those web apps, including debugging Django page templates.

Environments

The Python extension automatically detects Python interpreters that are installed in standard locations. It also detects conda environments as well as virtual environments in the workspace folder. See Configuring Python environments.

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

(Video) How to setup Python for VSCode in 2023 in 5mins! | Install Python and Setup VSCode for Windows 10

Python in Visual Studio Code (3)

The Status Bar also indicates if no interpreter is selected:

Python in Visual Studio Code (4)

The selected environment is used for IntelliSense, auto-completions, linting, formatting, and any other language-related feature other than debugging. It is also activated when you run Python in a terminal.

To change the current interpreter, which includes switching to conda or virtual environments, select the interpreter name on the Status Bar or use the Python: Select Interpreter command.

Python in Visual Studio Code (5)

VS Code prompts you with a list of detected environments as well as any you've added manually to your user settings (see Configuring Python environments).

Installing packages

Packages are installed using the Terminal panel and commands like pip install <package_name> (Windows) and pip3 install <package_name> (macOS/Linux). VS Code installs that package into your project along with its dependencies. Examples are given in the Python tutorial as well as the Django and Flask tutorials.

Jupyter notebooks

If you open a Jupyter notebook file (.ipynb) in VS Code, you can use the Jupyter Notebook Editor to directly view, modify, and run code cells.

(Video) Powerful VSCode Tips And Tricks For Python Development And Design

Python in Visual Studio Code (6)

You can also convert and open the notebook as a Python code file. The notebook's cells are delimited in the Python file with #%% comments, and the Python extension shows Run Cell or Run All Cells CodeLens. Selecting either CodeLens starts the Jupyter server and runs the cell(s) in the Python interactive window:

Python in Visual Studio Code (7)

Opening a notebook as a Python file allows you to use all of VS Code's debugging capabilities. You can then save the notebook file and open it again as a notebook in the Notebook Editor, Jupyter, or even upload it to a service like Azure Notebooks.

Using either method, Notebook Editor or a Python file, you can also connect to a remote Jupyter server for running the code. For more information, see Jupyter support.

Testing

The Python extension supports testing with unittest and pytest.

To run tests, you enable one of the frameworks in settings. Each framework also has specific settings, such as arguments that identify paths and patterns for test discovery.

Once discovered, VS Code provides a variety of commands (on the Status Bar, the Command Palette, and elsewhere) to run and debug tests, including the ability to run individual test files and individual methods.

Configuration

The Python extension provides a wide variety of settings for its various features. These are described on their relevant topics, such as Editing code, Linting, Debugging, and Testing. The complete list is found in the Settings reference.

(Video) How to Run Python in Visual Studio Code on Windows 10 2022 Best IDE

Other popular Python extensions

The Microsoft Python extension provides all of the features described previously in this article. Additional Python language support can be added to VS Code by installing other popular Python extensions.

  1. Open the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)).
  2. Filter the extension list by typing 'python'.

The extensions shown above are dynamically queried. Click on an extension tile above to read the description and reviews to decide which extension is best for you. See more in the Marketplace.

Next steps

  • Python Hello World tutorial - Get started with Python in VS Code.
  • Editing Python - Learn about auto-completion, formatting, and refactoring for Python.
  • Basic Editing - Learn about the powerful VS Code editor.
  • Code Navigation - Move quickly through your source code.
  • Django tutorial
  • Flask tutorial

10/8/2021

FAQs

Can you use Visual Studio code for Python? ›

Visual Studio Code is a free source code editor that fully supports Python and useful features such as real-time collaboration. It's highly customizable to support your classroom the way you like to teach.

Is Python good in VS Code? ›

PyCharm and VS Code are both excellent tools for writing Python code. However, it is vital to note that while PyCharm is an IDE, VS Code is a code editor that provides a similar experience to an IDE through extensions.

Is VS Code good for Python or PyCharm? ›

PyCharm is the way to go if you want a powerful, focused, and well-configured Python development environment. However, VS Code is preferred if you want something lightweight with the ability to customize. Both are excellent tools that, depending on how you choose to utilize them, can be used for a variety of tasks.

Which Visual Studio is best for Python? ›

The most popular IDEs for experienced Python developers are PyCharm and Visual Studio Code.

How do I get Visual Studio to recognize Python? ›

Select the Add Environment command in the Python Environments window or the Python toolbar, select the Python installation tab, indicate which interpreters to install, and select Install. You can also manually install any of the interpreters listed in the table below outside of the Visual Studio installer.

Which IDE is better than VS Code? ›

SlickEdit provides software developers with the multi-language, multi-platform code-editing capabilities they need to write better code, faster. Categories in common with Visual Studio Code: Text Editor.

Should I use Visual Studio or VS Code for Python? ›

If you need to collaborate with team members on development or debugging, then Visual Studio is the better choice. If you need to do serious code analysis or performance profiling, or debug from a snapshot, then Visual Studio Enterprise will help you. VS Code tends to be popular in the data science community.

What language is VS Code best for? ›

Hundreds of programming languages supported

In Visual Studio Code, we have support for almost every major programming language. Several ship in the box, for example, JavaScript, TypeScript, CSS, and HTML but more rich language extensions can be found in the VS Code Marketplace.

Which IDE to use for Python? ›

One of the best (and only) full-featured, dedicated IDEs for Python is PyCharm. Available in both paid (Professional) and free open-source (Community) editions, PyCharm installs quickly and easily on Windows, Mac OS X, and Linux platforms. Out of the box, PyCharm supports Python development directly.

What is the official Python IDE? ›

IDLE is Python's Integrated Development and Learning Environment.

Is VS Code good for beginners? ›

While marketing primarily to professional programmers, VS Code is an excellent editor for students and other learner just getting started with HTML and CSS. This course focuses mainly on those students and learners who in the beginner to intermediate stages of learning to code with HTML, CSS, and JavaScript.

What are the disadvantages of VS Code? ›

Cons: Memory & CPU usage. VS code uses lots of CPU & RAM. Because it is built using Electron which is simply a web framework.

Is VS Code a full IDE? ›

Visual Studio or VS code? If you weren't knowing it before then let me tell you that VS code or Visual Studio Code is an editor while Visual Studio is an IDE (Integrated Development Environment).

Is PyCharm faster than VS Code? ›

Microsoft's Visual Studio Code is much faster as compared to PyCharm. It is extremely lightweight as compared to PyCharm. When it comes to modular approach of wiring code, Visual Studio Code is a winner. Microsoft's IDE has a wide range of extensions, add-ons, and other libraries.

Do developers use VS Code? ›

In the Stack Overflow 2021 Developer Survey, Visual Studio Code was ranked the most popular developer environment tool among 82,000 respondents, with 70% reporting that they use it.

Why is VS Code so popular? ›

Robust and extensible architecture. Architecturally, Visual Studio Code combines the best of web, native, and language-specific technologies. Using Electron, VS Code combines web technologies such as JavaScript and Node. js with the speed and flexibility of native apps.

What is difference between Visual Studio and Visual Studio Code? ›

Visual Studio vs Visual Studio Code - Differences

Visual Studio is an Integrated Development Environment, also known as an IDE. Visual Studio Code is a code editor. A developer can easily edit their code. VS is slower when it comes to performing across different platforms.

How to install Python 3 in VS Code? ›

Click on the Extension button on the left-hand menu bar. The Marketplace for VS Code Extensions should open up. Search for python in the search bar. Choose the Python extension provided by Microsoft and click on “Install”.

How to install pip in Python? ›

Ensure you can run pip from the command line

Run python get-pip.py . 2 This will install or upgrade pip. Additionally, it will install setuptools and wheel if they're not installed already. Be cautious if you're using a Python install that's managed by your operating system or another package manager.

Do I need to Install Python separately with VS Code? ›

You must install a Python interpreter yourself separately from the extension. For a quick install, use Python from python.org and install the extension from the VS Code Marketplace. Once you have a version of Python installed, activate it using the Python: Select Interpreter command.

Which Python extension for Visual Studio Code? ›

Python VScode extension developed by Microsoft is a feature-rich Python extension that is completely free. VScode will automatically suggest this extension when you start to create a . py file. Its IntelliSense feature allows useful functionalities like code auto-completion, navigation, and syntax checking.

How do I run Python instead of python3 in VS Code? ›

In vs code press shift + command + p. On the top it has an option “Enter interpreter path”. Select that and enter the path to the python version you downloaded and installed on your machine. Hope this helps you friends.

Do I need pip to install Python? ›

pip is a recommended tool for installing Python packages. For example, if you need to install an external package/library, say requests, you have to install it first using pip.

Do I need to download pip for Python? ›

PIP is automatically installed with Python 2.7.9+ and Python 3.4+ and it comes with the virtualenv and pyvenv virtual environments.

Videos

1. Getting Started with Python in Visual Studio Code
(Microsoft Visual Studio)
2. 👩‍💻 Python for Beginners Tutorial
(Kevin Stratvert)
3. How to Run Python in Visual Studio Code on Windows 10 [2022] | Run Sample Python Program
(Amit Thinks)
4. Настройка VS Code для программирования на Python | Базовый курс. Программирование на Python
(Sweet Coder)
5. How to Set Up Python in Visual Studio Code on Windows 10
(ProgrammingKnowledge)
6. you NEED to use VS Code RIGHT NOW!!
(NetworkChuck)
Top Articles
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated: 04/20/2023

Views: 6069

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.