Visual Studio Code Python

  



Python Tools for Visual Studio is a completely free extension, developed and supported by Microsoft with contributions from the community. Visit our Github page to see or participate in PTVS development. Visual Studio Community 2019 Free, fully-featured IDE for students, open-source and individual. Python in Visual Studio Code Install Python and the Python extension #. The tutorial guides you through installing Python and using the extension. Insiders program #. The Insiders program allows you to try out and automatically install new versions of the Python. Run Python code #. From the Debug view, select the Configuration drop-down, then Add Configuration, and select Python: Visual Studio Code will create a debug configuration file under the current folder called.vscode/launch.json, which allows you to setup specific Python configurations as well as settings for debugging specific apps, like Django and Flask. Afterwards, open Visual Studio Code and left-click on the Visual Studio Code interpreter shown in Visual Studio Code at the bottom left: Choose a virtual environment that pops up in a dropdown of the settings window, and you are done. Mind the answer of @RamiMa. Search results for 'python', Visual Studio Code on marketplace.visualstudio.com.

  1. Visual Studio Code Python Intellisense Not Working
  2. Visual Studio Code Python Setup
  3. Visual Studio Code Python Terminal

By default the extension uses the Python interpreter available in the current path. You could always change this by specifying the path to the interpreter in the user or worksapce settings.

Selecting an interpreter

Configuring the extension to use a particular interpreter is now easy.All you now need to do is make use of the command Select Workspace Interpreter from the command palette and select an interpreter from the list.If the interpreter you are looking for isn’t listed here, then please proceed to the next section and enter it manually.

Manual Configuration

Visual Studio Code Python

Python Version used for Intellisense, Autocomplete, Linting, Formatting, etc
The same python interpreter is used for intellisense, autocomplete, linting, formatting, etc. (everything other than debugging). The standard interpreter used is the first “python” interpreter encountered in the current path.

Opencv

If a specific version is to be used, then configure the path to the python interpreter in the User or Workspace Settings file (settings.json) as follows.
Ensure to specify the fully qualified name of the python executable (Mac and Linux supported).

Virtual Environments

There are two approaches to to getting this extension working in a particular Virtual Environment.

Option 1: Ensure the path to the python interpreter is set in python.pythonPath as defined previously.

Note: Do remember to configure the pythonPath in launch.json as well.

Finally, restart VS Code, necessary for intellisense to work (future release will not require a restart)
Ensure the libraries/modules you plan on using for linting are also installed within this virtual environment.

Option 2: Activate the Virtual Environment from your Terminal/Command Window and then launch VS Code.

  1. Ensure none of the Python paths are configured in the settings.json file (leave them to their defaults).
  2. Open your terminal (command) window and activate the relevant Python environment
  3. Close all instances of VS Code
  4. Next, launch VS Code from that same terminal (command window) session
Visual

Python Version used for debugging

Details on configuration settings for debugging can be found here Debugging.

Configuring the version of the python executable is no longer necessary.
Provided the setting python.pythonPath in settings.json (see above) has been configured correctly, the debugger will use this same setting.

This is made possible by setting the value of the pythonPath setting to ${config.python.pythonPath}. I.e. the debugger merely references the pythonPath setting from the settings.json file.

Relative Paths to Python Interpreter

Unfortunately use of relative paths when configuring the interpreter in settings.json will not work with the debugger. Hence the solution is to provide the fully qualified path.This could be achieved with the use of simple variables such as the following:
Where ${workspaceRoot} resolves to the current work space (project) directory.

Environment Variables

Similar to the use of ${workspaceRoot}, environment variables could be used in configuring the path to the python interpreter.Where ${env.PYTHONPATH} resolves to the value of the environment variable xyz.

-->

By default, installing the Python development workload in Visual Studio 2017 and later also installs Python 3 (64-bit). You can optionally choose to install 32-bit and 64-bit versions of Python 2 and Python 3, along with Miniconda (Visual Studio 2019) or Anaconda 2/Anaconda 3 (Visual Studio 2017), as described in Installation.

Alternately, you can install standard python interpreters from the Add Environment dialog. 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. For example, if you installed Anaconda 3 before installing Visual Studio, you don't need to install it again through the Visual Studio installer. You can also install an interpreter manually if, for example, a newer version of available that doesn't yet appear in the Visual Studio installer.

Note

Visual Studio supports Python version 2.7, as well as version 3.5 to 3.7. While it is possible to use Visual Studio to edit code written in other versions of Python, those versions are not officially supported and features such as IntelliSense and debugging might not work.

Visual Studio Code Python

For Visual Studio 2015 and earlier, you must manually install one of the interpreters.

Visual Studio (all versions) automatically detects each installed Python interpreter and its environment by checking the registry according to PEP 514 - Python registration in the Windows registry. Python installations are typically found under HKEY_LOCAL_MACHINESOFTWAREPython (32-bit) and HKEY_LOCAL_MACHINESOFTWAREWOW6432NodePython (64-bit), then within nodes for the distribution such as PythonCore (CPython) and ContinuumAnalytics (Anaconda).

Visual Studio Code Python Intellisense Not Working

If Visual Studio does not detect an installed environment, see Manually identify an existing environment.

Visual Studio shows all known environments in the Python Environments window, and automatically detects updates to existing interpreters.

InterpreterDescription
CPythonThe 'native' and most commonly-used interpreter, available in 32-bit and 64-bit versions (32-bit recommended). Includes the latest language features, maximum Python package compatibility, full debugging support, and interop with IPython. See also: Should I use Python 2 or Python 3?. Note that Visual Studio 2015 and earlier do not support Python 3.6+ and can give errors like Unsupported python version 3.6. Use Python 3.5 or earlier instead.
IronPythonA .NET implementation of Python, available in 32-bit and 64-bit versions, providing C#/F#/Visual Basic interop, access to .NET APIs, standard Python debugging (but not C++ mixed-mode debugging), and mixed IronPython/C# debugging. IronPython, however, does not support virtual environments.
AnacondaAn open data science platform powered by Python, and includes the latest version of CPython and most of the difficult-to-install packages. We recommend it if you can't otherwise decide.
PyPyA high-performance tracing JIT implementation of Python that's good for long-running programs and situations where you identify performance issues but cannot find other resolutions. Works with Visual Studio but with limited support for advanced debugging features.
JythonAn implementation of Python on the Java Virtual Machine (JVM). Similar to IronPython, code running in Jython can interact with Java classes and libraries, but may not be able to use many libraries intended for CPython. Works with Visual Studio but with limited support for advanced debugging features.

Developers that want to provide new forms of detection for Python environments, see PTVS Environment Detection (github.com).

Visual Studio Code Python Setup

Move an interpreter

If you move an existing interpreter to a new location using the file system, Visual Studio doesn't automatically detect the change.

  • If you originally specified the location of the interpreter through the Python Environments window, then edit its environment using the Configure tab in that window to identify the new location. See Manually identify an existing environment.

  • If you installed the interpreter using an installer program, then use the following steps to reinstall the interpreter in the new location:

    1. Restore the Python interpreter to its original location.
    2. Uninstall the interpreter using its installer, which clears the registry entries.
    3. Reinstall the interpreter at the desired location.
    4. Restart Visual Studio, which should auto-detect the new location in place of the old location.

Following this process ensures that the registry entries that identify the interpreter's location, which Visual Studio uses, are properly updated. Using an installer also handles any other side effects that may exist.

Code

Visual Studio Code Python Terminal

See also