Useful commands
Open the Command Palette (Command+Shift+P on macOS and Ctrl+Shift+P on Windows/Linux) and type in one of the following commands:
Command | Description |
|
Switch between Python interpreters, versions, and environments. |
|
Start an interactive Python REPL using the selected interpreter in the VS Code terminal. |
|
Runs the active Python file in the VS Code terminal. You can also run a Python file by right-clicking on the file and selecting
. |
|
Formats code using the provided formatter in the
file. |
|
Select a test framework and configure it to display the Test Explorer. |
To see all available Python commands, open the Command Palette and type
Python
. For Jupyter extension commands, just type
Jupyter
.
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.
GitHub Copilot provides suggestions for languages beyond Python and a wide variety of frameworks, including JavaScript, TypeScript, Ruby, Go, C# and C++.
You can learn more about how to get started with Copilot in the Copilot documentation.
Create a Python source code file
From the File Explorer toolbar, select the New File button on the
hello
folder:
Name the file
hello.py
, and VS Code will automatically open it in the editor:
By using the
.py
file extension, you tell VS Code to interpret this file as a Python program, so that it evaluates the contents with the Python extension and the selected interpreter.
Note: The File Explorer toolbar also allows you to create folders within your workspace to better organize your code. You can use the New folder button to quickly create a folder.
Now that you have a code file in your Workspace, enter the following source code in
hello.py
:
msg = "Roll a dice" print(msg)
When you start typing
IntelliSense and auto-completions work for standard Python modules as well as other packages you’ve installed into the environment of the selected Python interpreter. It also provides completions for methods available on object types. For example, because the
msg
variable contains a string, IntelliSense provides string methods when you type
msg.
:
Finally, save the file (⌘S (Windows, Linux Ctrl+S)). At this point, you’re ready to run your first Python file in VS Code.
For full details on editing, formatting, and refactoring, see Editing code. The Python extension also has full support for Linting.
Environment variables
Environment variable definitions file
An environment variable definitions file is a text file containing key-value pairs in the form of
environment_variable=value
, with used for comments. Multiline values aren’t supported, but references to previously defined environment variables are allowed. Environment variable definitions files can be used for scenarios such as debugging and tool execution (including linters, formatters, IntelliSense, and testing tools), but aren’t applied to the terminal.
Note: Environment variable definitions files are not necessarily cross-platform. For instance, while Unix uses
as a path separator in environment variables, Windows uses. There is no normalization of such operating system differences, and so you need to make sure any environment definitions file use values that are compatible with your operating system.
By default, the Python extension looks for and loads a file named
.env
in the current workspace folder, then applies those definitions. The file is identified by the default entry
"python.envFile": "${workspaceFolder}/.env"
in your user settings (see General Python settings). You can change the
python.envFile
setting at any time to use a different definitions file.
Note: Environment variable definitions files are not used in all situations where environment variables are available for use. Unless Visual Studio Code documentation states otherwise, these only affect certain scenarios as per their definition. For example, the extension doesn’t use environment variable definitions files when resolving setting values.
A debug configuration also contains an
envFile
property that also defaults to the
.env
file in the current workspace (see Debugging – Set configuration options). This property allows you to easily set variables for debugging purposes that replace variables specified in the default
.env
file.
For example, when developing a web application, you might want to easily switch between development and production servers. Instead of coding the different URLs and other settings into your application directly, you could use separate definitions files for each. For example:
dev.env file
# dev.env - development configuration # API endpoint MYPROJECT_APIENDPOINT=https://my.domain.com/api/dev/ # Variables for the database MYPROJECT_DBURL=https://my.domain.com/db/dev MYPROJECT_DBUSER=devadmin MYPROJECT_DBPASSWORD=!dfka**213=
prod.env file
# prod.env - production configuration # API endpoint MYPROJECT_APIENDPOINT=https://my.domain.com/api/ # Variables for the database MYPROJECT_DBURL=https://my.domain.com/db/ MYPROJECT_DBUSER=coreuser MYPROJECT_DBPASSWORD=kKKfa98*11@
You can then set the
python.envFile
setting to
${workspaceFolder}/prod.env
, then set the
envFile
property in the debug configuration to
${workspaceFolder}/dev.env
.
Note: When environment variables are specified using multiple methods, be aware that there is an order of precedence. All
env
variables defined in the
launch.json
file will override variables contained in the
.env
file, specified by the
python.envFile
setting (user or workspace). Similarly,
env
variables defined in the
launch.json
file will override the environment variables defined in the
envFile
that are specified in
launch.json
.
Use of the PYTHONPATH variable
The PYTHONPATH environment variable specifies additional locations where the Python interpreter should look for modules. In VS Code, PYTHONPATH can be set through the terminal settings (
terminal.integrated.env.*
) and/or within an
.env
file.
When the terminal settings are used, PYTHONPATH affects any tools that are run within the terminal by a user, as well as any action the extension performs for a user that is routed through the terminal such as debugging. However, in this case when the extension is performing an action that isn’t routed through the terminal, such as the use of a linter or formatter, then this setting won’t have an effect on module look-up.
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.
- Open the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)).
- 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.
Start VS Code in a workspace folder
By starting VS Code in a folder, that folder becomes your “workspace”.
Using a command prompt or terminal, create an empty folder called “hello”, navigate into it, and open VS Code (
code
) in that folder () by entering the following commands:
mkdir hello cd hello code .
Note: If you’re using an Anaconda distribution, be sure to use an Anaconda command prompt.
Alternately, you can create a folder through the operating system UI, then use VS Code’s File > Open Folder to open the project folder.
Tips and Tricks for Efficient Python Development in VSCode
VSCode comes with awesome Python development features and extensions. We can customize them to our needs and improve the productivity. In this section, we will learn about tips and tricks for efficient Python development.
- Getting started: Help > Get Started. Learn about VSCode’s customization and features by following guided tutorials.
- Command Palette: access entire all available commands by using the Keyboard shortcut: Ctrl+Shift+P. By writing keywords, we can access specific commands.
- Keyboard shortcuts: better than command palettes. We can modify keyboard shortcuts or memorize them by using keyboard reference sheets. It will help us access the commands directly, instead of searching with the keyword.
- Command line: launch the VSCode editor through the command line interface by typing `code .`. We can also customize how the editor is launched by adding additional arguments.
- Errors and warnings: quick jump to errors and warnings in a project by using the keyboard shortcut: Ctrl+Shift+M. We can also cycle through the error with F8 or Shift+F8.
- Customization: VSCode allows us to customize themes, keyboard shortcuts, JSON validation, debugging settings, fonts, and many more. It is a fully customizable IDE.
- Extensions: other Python extensions improve our development experience. Look for popular extensions on the Visual Studio Marketplace.
- Multi cursor selection: is a lifesaver. Add cursors at arbitrary positions by using Alt+Click. It will allow us to modify multiple lines of code at once. We can also use Ctrl+Shift+L to modify all occurrences of the current selection.
- Search and modify: this is the best tool for searching and modifying multiple expressions at once. We can also rename the symbol by selecting the symbol and typing F2.
- Git integration: allows us to perform all Git-related tasks within IDE. It provides an easy-to-use GUI for diff, views, staging, branching, committing, merging, and more.
- Code Snippets: is our best friend. Just like Autohotkey, we are creating templates for repeating code patterns. To create a custom code snippet, select File > Preferences > Configure User Snippets and then select the language.
- GitHub Copilot: is a winner extension for all kinds of development. It enhances the coding experience with artificial intelligence (AI) by suggesting lines of code or entire functions.
Bonus: sync your settings by logging into your GitHub account. It will sync your settings across all of the machines.
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.
Note: To help get you started with Python development, you can use the Python profile template that includes useful extensions, settings, and Python code snippets.
Once you have a version of Python installed, select 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.
Working with Python interpreters
Select and activate an environment
The Python extension tries to find and then select what it deems the best environment for the workspace. If you would prefer to select a specific environment, use the Python: Select Interpreter command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).
Note: If the Python extension doesn’t find an interpreter, it issues a warning. On macOS 12.2 and older, the extension also issues a warning if you’re using the OS-installed Python interpreter as it is known to have compatibility issues. In either case, you can disable these warnings by setting
python.disableInstallationCheck
to
true
in your user settings.
The Python: Select Interpreter command displays a list of available global environments, conda environments, and virtual environments. (See the Where the extension looks for environments section for details, including the distinctions between these types of environments.) The following image, for example, shows several Anaconda and CPython installations along with a conda environment and a virtual environment (
env
) that’s located within the workspace folder:
Note: On Windows, it can take a little time for VS Code to detect available conda environments. During that process, you may see “(cached)” before the path to an environment. The label indicates that VS Code is presently working with cached information for that environment.
If you have a folder or a workspace open in VS Code and you select an interpreter from the list, the Python extension will store that information internally. This ensures that the same interpreter will be used when you reopen the workspace.
The selected environment is used by the Python extension for running Python code (using the Python: Run Python File in Terminal command), providing language services (auto-complete, syntax checking, linting, formatting, etc.) when you have a
.py
file open in the editor, and opening a terminal with the Terminal: Create New Terminal command. In the latter case, VS Code automatically activates the selected environment.
Tip: To prevent automatic activation of a selected environment, add
"python.terminal.activateEnvironment": false
to your
settings.json
file (it can be placed anywhere as a sibling to the existing settings).
Tip: If the activate command generates the message “Activate.ps1 is not digitally signed. You cannot run this script on the current system.”, then you need to temporarily change the PowerShell execution policy to allow scripts to run (see About Execution Policies in the PowerShell documentation):
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
Note: By default, VS Code uses the interpreter selected for your workspace when debugging code. You can override this behavior by specifying a different path in the
python
property of a debug configuration. See Choose a debugging environment.
The selected interpreter version will show on the right side of the Status Bar.
The Status Bar also reflects when no interpreter is selected.
In either case, clicking this area of the Status Bar is a convenient shortcut for the Python: Select Interpreter command.
Tip: If you have any problems with VS Code recognizing a virtual environment, please file an issue so we can help determine the cause.
Manually specify an interpreter
If VS Code doesn’t automatically locate an interpreter you want to use, you can browse for the interpreter on your file system or provide the path to it manually.
You can do so by running the Python: Select Interpreter command and select the Enter interpreter path… option that shows on the top of the interpreters list:
You can then either enter the full path of the Python interpreter directly in the text box (for example, “.venv/Scripts/python.exe”), or you can select the Find… button and browse your file system to find the python executable you wish to select.
If you want to manually specify a default interpreter that will be used when you first open your workspace, you can create or modify an entry for the
python.defaultInterpreterPath
setting.
Note: Changes to the
python.defaultInterpreterPath
setting are not picked up after an interpreter has already been selected for a workspace; any changes to the setting will be ignored once an initial interpreter is selected for the workspace.
Additionally, if you’d like to set up a default interpreter to all of your Python applications, you can add an entry for
python.defaultInterpreterPath
manually inside your User Settings. To do so, open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and enter Preferences: Open User Settings. Then set
python.defaultInterpreterPath
, which is in the Python extension section of User Settings, with the appropriate interpreter.
How the extension chooses an environment automatically
If an interpreter hasn’t been specified, then the Python extension automatically selects the interpreter with the highest version in the following priority order:
- Virtual environments located directly under the workspace folder.
- Virtual environments related to the workspace but stored globally. For example, Pipenv or Poetry environments that are located outside of the workspace folder.
-
Globally installed interpreters. For example, the ones found in
/usr/local/bin
,
C:\\python38
, etc.
Note: The interpreter selected may differ from what
python
refers to in your terminal.
If Visual Studio Code doesn’t locate your interpreter automatically, you can manually specify an interpreter.
Where the extension looks for environments
The extension automatically looks for interpreters in the following locations, in no particular order:
-
Standard install paths such as
/usr/local/bin
,
/usr/sbin
,
/sbin
,
c:\\python36
, etc. - Virtual environments located directly under the workspace (project) folder.
-
Virtual environments located in the folder identified by the
python.venvPath
setting (see General Python settings), which can contain multiple virtual environments. The extension looks for virtual environments in the first-level subfolders of
venvPath
. -
Virtual environments located in a
~/.virtualenvs
folder for virtualenvwrapper. - Interpreters created by pyenv, Pipenv, and Poetry.
-
Virtual environments located in the path identified by
WORKON_HOME
(as used by virtualenvwrapper). -
Conda environments found by
conda env list
. Conda environments which do not have an interpreter will have one installed for them upon selection. -
Interpreters installed in a
.direnv
folder for direnv under the workspace folder.
Environments and Terminal windows
After using Python: Select Interpreter, that interpreter is applied when right-clicking a file and selecting Python: Run Python File in Terminal. The environment is also activated automatically when you use the Terminal: Create New Terminal command unless you change the
python.terminal.activateEnvironment
setting to
false
.
Please note that launching VS Code from a shell in which a specific Python environment is activated doesn’t automatically activate that environment in the default Integrated Terminal.
Note: conda environments cannot be automatically activated in the integrated terminal if PowerShell is set as the integrated shell. See Integrated terminal – Terminal profiles for how to change the shell.
Changing interpreters with the Python: Select Interpreter command doesn’t affect terminal panels that are already open. Thus, you can activate separate environments in a split terminal: select the first interpreter, create a terminal for it, select a different interpreter, then use the split button (⌘\ (Windows, Linux Ctrl+Shift+5)) in the terminal title bar.
Choose a debugging environment
By default, the debugger will use the Python interpreter chosen with the Python extension. However, if there is a
python
property specified in the debug configuration of
launch.json
, it takes precedence. If this property is not defined, it will fall back to using the Python interpreter path selected for the workspace.
For more details on debug configuration, see Debugging configurations.
Git Integration
VSCode comes with built-in Git integration. No more writing Git commands on terminals. Git integration provides a user-friendly GUI and helpful functions for diff, views, staging, branching, committing, merge, and more.
Check out our Git Cheat Sheet to learn about the various Git commands and functionalities.
Note: To enable Git integration, you need to install Git from official site.
Initializing Git
We can access it through the action bar or by using the keyboard shortcut: Ctrl + Shift + G. Before we start committing, we need to initialize the repository.
Git Commit
After that, add and commit the changes with the message. It is that simple.
Create a GitHub repository and push the code
You can even create a GitHub repository and push your code to a remote server by logging into your GitHub account.
Private GitHub repository
We have created a GitHub private repository of Python files and folders.
You can now simply commit and push the changes to the remote server without leaving the VSCode.
Follow our Github and Git tutorial to learn everything about Git and GitHub.
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.
- Open the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)).
- 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.
Create a virtual environment
A best practice among Python developers is to use a project-specific
virtual environment
. Once you activate that environment, any packages you then install are isolated from other environments, including the global interpreter environment, reducing many complications that can arise from conflicting package versions. You can create non-global environments in VS Code using Venv or Anaconda with Python: Create Environment.
Open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)), start typing the Python: Create Environment command to search, and then select the command.
The command presents a list of environment types, Venv or Conda. For this example, select Venv.
The command then presents a list of interpreters that can be used for your project. Select the interpreter you installed at the beginning of the tutorial.
After selecting the interpreter, a notification will show the progress of the environment creation and the environment folder (
/.venv
) will appear in your workspace.
Ensure your new environment is selected by using the Python: Select Interpreter command from the Command Palette.
Note: For additional information about virtual environments, or if you run into an error in the environment creation process, see Environments.
More Python resources
- Getting Started with Python in VS Code – Learn how to edit, run, and debug code in VS Code.
- Virtual Environments and Packages (Python.org) – Learn more about virtual environments and packages.
- Installing Python Modules (Python.org) – Learn how to install Python modules.
- Python tutorial (Python.org) – Learn more about the Python language.
FAQs
Is VS Code a reliable tool for Python?
Yes. VS Code helps edit Python source codes and has reliable features that boost development.
Why is VS Code not recognizing your Python imports?
This most likely results from choosing the wrong interpreter when coding. Ensure you select and install packages correctly to avoid compatibility issues.
Is VS Code a better IDE for Python compared to PyCharm?
Yes. VS Code has a wider array of features, making customization easier and more versatile.
A quick introduction to the Visual Studio Code
Visual Studio Code is a lightweight source code editor. The Visual Studio Code is often called VS Code. The VS Code runs on your desktop. It’s available for Windows, macOS, and Linux.
VS Code comes with many features such as IntelliSense, code editing, and extensions that allow you to edit Python source code effectively. The best part is that the VS Code is open-source and free.
Besides the desktop version, VS Code also has a browser version that you can use directly in your web browser without installing it.
This tutorial teaches you how to set up Visual Studio Code for a Python environment so that you can edit, run, and debug Python code.
Next steps
To learn how to build web apps with popular Python web frameworks, see the following tutorials:
There is much more to explore with Python in Visual Studio Code:
- Python profile template – Create a new profile with a curated set of extensions, settings, and snippets
- Editing code – Learn about autocomplete, IntelliSense, formatting, and refactoring for Python.
- Linting – Enable, configure, and apply a variety of Python linters.
- Debugging – Learn to debug Python both locally and remotely.
- Testing – Configure test environments and discover, run, and debug tests.
- Settings reference – Explore the full range of Python-related settings in VS Code.
Python environments in VS Code
An “environment” in Python is the context in which a Python program runs that consists of an interpreter and any number of installed packages.
Note: If you’d like to become more familiar with the Python programming language, review More Python resources.
Creating environments
Using the Create Environment command
To create local environments in VS Code using virtual environments or Anaconda, you can follow these steps: open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)), search for the Python: Create Environment command, and select it.
The command presents a list of environment types: Venv or Conda.
If you are creating an environment using Venv, the command presents a list of interpreters that can be used as a base for the new virtual environment.
If you are creating an environment using Conda, the command presents a list of Python versions that can be used for your project.
After selecting the desired interpreter or Python version, a notification will show the progress of the environment creation and the environment folder will appear in your workspace.
Note: The command will also install necessary packages outlined in a requirements/dependencies file, such as
requirements.txt
,
pyproject.toml
, or
environment.yml
, located in the project folder. It will also add a
.gitignore
file to the virtual environment to help prevent you from accidentally committing the virtual environment to source control.
Create a virtual environment in the terminal
If you choose to create a virtual environment manually, use the following command (where “.venv” is the name of the environment folder):
# macOS/Linux # You may need to run `sudo apt-get install python3-venv` first on Debian-based OSs python3 -m venv .venv # Windows # You can also use `py -3 -m venv .venv` python -m venv .venv
Note: To learn more about the
venv
module, read Creation of virtual environments on Python.org.
When you create a new virtual environment, a prompt will be displayed in VS Code to allow you to select it for the workspace.
Tip: Make sure to update your source control settings to prevent accidentally committing your virtual environment (in for example
.gitignore
). Since virtual environments are not portable, it typically does not make sense to commit them for others to use.
Create a conda environment in the terminal
The Python extension automatically detects existing conda environments. We recommend you install a Python interpreter into your conda environment, otherwise one will be installed for you after you select the environment. For example, the following command creates a conda environment named
env-01
with a Python 3.9 interpreter and several libraries:
conda create -n env-01 python=3.9 scipy=0.15.0 numpy
Note: For more information on the conda command line, you can read Conda environments.
Additional notes:
-
If you create a new conda environment while VS Code is running, use the refresh icon on the top right of the Python: Select Interpreter window; otherwise you may not find the environment there.
-
To ensure the environment is properly set up from a shell perspective, use an Anaconda prompt and activate the desired environment. Then, you can launch VS Code by entering the
code .
command. Once VS Code is open, you can select the interpreter either by using the Command Palette or by clicking on the status bar. -
Although the Python extension for VS Code doesn’t currently have direct integration with conda
environment.yml
files, VS Code itself is a great YAML editor. -
Conda environments can’t be automatically activated in the VS Code Integrated Terminal if the default shell is set to PowerShell. To change the shell, see Integrated terminal – Terminal profiles.
-
You can manually specify the path to the
conda
executable to use for activation (version 4.4+). To do so, open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and run Preferences: Open User Settings. Then set
python.condaPath
, which is in the Python extension section of User Settings, with the appropriate path.
Questions, issues, feature requests, and contributions
- If you have a question about how to accomplish something with the extension, please ask on Stack Overflow
- If you come across a problem with the extension, please file an issue
- Contributions are always welcome! Please see our contributing guide for more details
-
Any and all feedback is appreciated and welcome!
- If someone has already filed an issue that encompasses your feedback, please leave a 👍/👎 reaction on the issue
- Otherwise please start a new discussion
- If you’re interested in the development of the extension, you can read about our development process
Testing
The Python extension supports testing with Python’s built-in unittest framework and pytest.
In order to run tests, you must enable one of the supported testing frameworks in the settings of your project. Each framework has its own specific settings, such as arguments for identifying the paths and patterns for test discovery.
Once the tests have been discovered, VS Code provides a variety of commands (on the Status Bar, the Command Palette, and elsewhere) to run and debug tests. These commands also allow you to run individual test files and methods
Installed extensions
The Python extension will automatically install the following extensions by default to provide the best Python development experience in VS Code:
- Pylance – to provide performant Python language support
- Python Debugger – to provide a seamless debug experience with debugpy
These extensions are optional dependencies, meaning the Python extension will remain fully functional if they fail to be installed. Any or all of these extensions can be disabled or uninstalled at the expense of some features. Extensions installed through the marketplace are subject to the Marketplace Terms of Use.
Data and telemetry
The Microsoft Python Extension for Visual Studio Code collects usage
data and sends it to Microsoft to help improve our products and
services. Read our
privacy statement to
learn more. This extension respects the
telemetry.enableTelemetry
setting which you can learn more about at
https://code.visualstudio.com/docs/supporting/faq#_how-to-disable-telemetry-reporting.
Summary: in this tutorial, you’ll learn how to set up Visual Studio Code for Python development.
Advance Your Python Coding Skills With VS Code
It’s important that you have the right resources geared toward your Python development journey. VS Code is one such tool. It provides a great place to start and grow your Python development knowledge. It hosts noteworthy features that are key in helping you master Python and data science in general. You can set up your VS Code to match your preferred workflow to enhance productivity.
So, what are your thoughts on integrating Python with VS Code? Are there any upgrades you would like to see on VS Code to help enhance your Python development environment? Let us know in the comments section below.
Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.
Testing
The Python extension supports testing with Python’s built-in unittest framework and pytest.
In order to run tests, you must enable one of the supported testing frameworks in the settings of your project. Each framework has its own specific settings, such as arguments for identifying the paths and patterns for test discovery.
Once the tests have been discovered, VS Code provides a variety of commands (on the Status Bar, the Command Palette, and elsewhere) to run and debug tests. These commands also allow you to run individual test files and methods
Jupyter notebooks
To enable Python support for Jupyter notebook files (
.ipynb
) in VS Code, you can install the Jupyter extension. The Python and Jupyter extensions work together to give you a great Notebook experience in VS Code, providing you the ability to directly view and modify code cells with IntelliSense support, as well as run and debug them.
You can also convert and open the notebook as a Python code file through the Jupyter: Export to Python Script command. The notebook’s cells are delimited in the Python file with
#%%
comments, and the Jupyter extension shows Run Cell or Run Below CodeLens. Selecting either CodeLens starts the Jupyter server and runs the cell(s) in the Python interactive window:
You can also connect to a remote Jupyter server to run your notebooks. For more information, see Jupyter support.
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 right side of the VS Code Status Bar:
The Status Bar also indicates if no interpreter is selected:
The selected environment is used for IntelliSense, auto-completions, linting, formatting, and any other language-related feature. It is also activated when you run or debug Python in a terminal, or when you create a new terminal with the Terminal: Create New Terminal command.
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.
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).
Install Python Extension
To make the VS Code work with Python, you need to install the Python extension from the Visual Studio Marketplace.
The following picture illustrates the steps:
- First, click the Extensions tab.
-
Second, type the
python
keyword on the search input. -
Third, click the
Python
extension. It’ll show detailed information on the right pane. - Finally, click the Install button to install the Python extension.
Now, you’re ready to develop the first program in Python.