Run Python In Visual Studio Code



  1. How To Run A Python Code In Visual Studio
  2. You Can Add A Custom Task To Do This. Here Is A Basic Custom Task For Python. { 'version': '0.1.0', 'command': 'c:\\\\Python34\\\\python',...

Python support is presently available only on Visual Studio for Windows; on Mac and Linux, Python support is available through Visual Studio Code. Download and run the latest Visual Studio installer for Windows (Python support is present in release 15.2 and later). Python support is presently available only on Visual Studio for Windows; on Mac and Linux, Python support is available through Visual Studio Code. Download and run the latest Visual Studio installer for Windows (Python support is present in release 15.2 and later).

Run Python In Visual Studio Code

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

You can then force Visual Studio to use a specific python runtime by reaching to your VS Code settings. Then, add a “python.path” property to your user or workspace settings. Choose user settings.

Capturing user input via Console Application whist debugging a Python application is possible when using a Terminal (console window) to capture the input. There are two options for using terminals in Visual Studio Code when debugging: Option 1: Use the Visual Studio Code Terminal (integrated terminal). The Python Extension for Visual Studio Code is fully open source and integrates other open source Python packages for developers to provide rich editing, debugging, and testing capabilities.

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

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.

How to run python in visual studio code

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.

How To Run A Python Code In Visual Studio

  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

Python Version used for debugging

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

Run Python In Visual Studio Code

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.

Visual

Relative Paths to Python Interpreter

How to run python code in visual studio code

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

You Can Add A Custom Task To Do This. Here Is A Basic Custom Task For Python. { 'version': '0.1.0', 'command': 'c:\\\\Python34\\\\python',...

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.