How do i change vs code in python?
This article discusses the helpful Python environments features available in Visual Studio Code. An "environment" in Python is the context in which a Python program runs and consists of an interpreter and any number of installed packages. Show
Python environmentsGlobal environmentsBy default, any Python interpreter installed runs in its own global environment. They aren't specific to a particular project. For example, if you just run Working in the global environment is an easy way to get started. If you install packages in that environment, though, in time it will become crowded and make it difficult to properly test an application. Virtual environmentsTo prevent such clutter, developers often create a virtual environment for a project. A virtual environment is a folder that contains a copy (or symlink) of a specific interpreter. When you install into a virtual environment, any packages you install are installed only in that subfolder. When you then run a Python program within that environment, you know that it's running against only those specific packages.
Python environment toolsOnce you activate your virtual environment, you’ll need to identify how to manage it and its accompanying packages. The following table explains how to use these Python environments:
Conda environmentsA conda environment is a Python environment that's managed using the Where the extension looks for environmentsThe extension automatically looks for interpreters in the following locations:
You can also manually specify an interpreter if Visual Studio Code doesn't locate your interpreter automatically.
The extension also loads an
environment variable definitions file identified by the Creating environmentsCreate a virtual environmentTo create a virtual environment, use the following command, where ".venv" is the name of the environment folder:
When you create a new virtual environment, a prompt will be displayed to allow you to select it for the workspace. This will add the path to the Python interpreter from the new virtual environment to your workspace settings. That environment will then be used when installing packages and running code through the Python extension. For examples of using virtual environment in projects, see the Python, Django, and Flask tutorials.
Create a conda environmentThe 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 with the Python 3.9 interpreter and several libraries, which VS Code then shows in the list of available interpreters:
In contrast, if you fail to specify an interpreter, as with For more information on the conda command line, see Conda environments (conda.io). Additional notes:
Work with Python interpretersSelect and activate an environmentBy default, the Python extension looks for and uses the first Python interpreter it finds in the system path. To select a specific environment, use the Python: Select Interpreter command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).
You can switch environments at any time; switching environments helps you test different parts of your project with different interpreters or library versions as needed. 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 (
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 so that the same interpreter will be used once you reopen the workspace. The Python extension uses the selected environment 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
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.
Manually specify an interpreterIf 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 clicking on 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 once you first open your workspace, you can create or modify an entry for For example:
You can also use
Additionally, if you'd like to set up a default interpreter to all of
your Python applications, you can add an entry for You
can also use an environment variable in the path setting using the syntax
By using an environment variable, you can easily transfer a project between operating systems where the paths are different, just be sure to set the environment variable on the operating system first. Environments and Terminal windowsAfter 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 However, launching VS Code from a shell in which a certain Python environment is activated doesn't automatically activate that environment in the default Integrated Terminal. Use the Terminal: Create New Terminal command after VS Code is running.
Any changes you make to an activated environment within the terminal are persistent. For example, using Changing interpreters with the Python: Select Interpreter command doesn't affect terminal panels that are already open. You can thus 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 environmentBy default, the debugger will use the Python interpreter you've selected with the Python extension. However, if you have a For more details on debug configuration, see Debugging configurations. Limited support for Python 2.7The Python extension no longer offers IntelliSense support for Python 2.7 with Jedi as it only supports Python 3 at this point. When using Python 2.7 with the Python extension you can customize the language server setting to either turn off auto-completions or select Pylance as your language server, as it may provide a good experience if the code is compatible enough with Python 3. We currently support selecting Python 2.7 as an interpreter in your workspace. Because Python 2.7 is no longer maintained as of January 2020, we strongly suggest you to upgrade your code to Python 3 as soon as you can. You can learn how to port your code to Python 3 if you need help. Environment variablesEnvironment variable definitions fileAn environment variable definitions file is a simple text file containing key-value pairs in the form of By default, the Python extension looks for and loads a file named
A debug configuration also contains an 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
prod.env file
You can then set the
Variable substitutionWhen defining an environment variable in a definitions file, you can use the value of any existing environment variable with the following general syntax:
where Within this syntax, the following rules apply:
Use of the PYTHONPATH variableThe 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 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. When PYTHONPATH is set using an If needed, you can set PYTHONPATH using both methods. An example of when to use PYTHONPATH would be if you have source code in a To solve this problem, you could add the path to
Then set
The value of PYTHONPATH can contain multiple locations separated by
Next steps
More Python resources
7/21/2022 How do I change the Python version in VS Code terminal?To do so, press CMD + SHIFT + P , type Python, and choose Select Interpreter.
How do I change my VS Code?To open the Settings editor, use the following VS Code menu command: On Windows/Linux - File > Preferences > Settings. On macOS - Code > Preferences > Settings.
How do I change the Python environment in Visual Studio?Right-click Python Environments in Solution Explorer and select Add Environment. In the Browse dialog that appears, navigate to and select the folder that contains the virtual environment, and select OK. If Visual Studio detects a requirements. txt file in that environment, it asks whether to install those packages.
How does Python interpreter choose VS Code?Install Python and the Python extension#
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.
|