Run Python Code In Visual Studio



Visual Studio Code Run Python File, Python Run in Visual Studio code, Python File Run in Visual Studio Code or VS Code.Pylint install Using Line in Command. You can run the file in an interactive mode in VSC code terminal by using the parameter -i: python -i pyfile.py.

-->

Once you've installed Python support in Visual Studio 2019, it's easy to run existing Python code in Visual Studio 2019 without creating a Visual Studio project.

Note

StudioVisual studio code for beginners

Visual Studio 2017 and earlier require you to create a Visual Studio project to run Python code, which you can easily do using a built-in project template. See Quickstart: Create a Python project from existing code

  1. For this walkthrough, you can use any folder with Python code that you like. To follow along with the example shown here, clone the gregmalcolm/python_koans GitHub repository to your computer using the command git clone https://github.com/gregmalcolm/python_koans in an appropriate folder.

  2. Launch Visual Studio 2019 and in the start window, select Open at the bottom of the Get started column. Alternately, if you already have Visual Studio running, select the File > Open > Folder command instead.

  3. Navigate to the folder containing your Python code, then choose Select Folder. If you're using the python_koans code, make sure to select the python3 folder within the clone folder.

  4. Visual Studio displays the folder in Solution Explorer in what's called Folder View. You can expand and collapse folders using the arrows on the left edges of the folder names:

  5. When opening a Python folder, Visual Studio creates several hidden folders to manage settings related to the project. To see these folders (and any other hidden files and folders, such as the .git folder), select the Show All Files toolbar button:

  6. To run the code, you first need to identify the startup or primary program file. In the example shown here, the startup file contemplate-koans.py. Right-click that file and select Set as Startup Item.

    Important

    If your startup item is not located in the root of the folder you opened, you must also add a line to the launch configuration JSON file as described in the section, Set a working directory.

  7. Run the code by pressing Ctrl+F5 or selecting Debug > Start without Debugging. You can also select the toolbar button that shows the startup item with a play button, which runs code in the Visual Studio debugger. In all cases, Visual Studio detects that your startup item is a Python file, so it automatically runs the code in the default Python environment. (That environment is shown to the right of the startup item on the toolbar.)

  8. The program's output appears in a separate command window:

  9. To run the code in a different environment, select that environment from the drop-down control on the toolbar, then launch the startup item again.

  10. To close the folder in Visual Studio, select the File > Close folder menu command.

Run

Set a working directory

Vs Code Python Setup

By default, Visual Studio runs a Python project opened as a folder in the root of that same folder. The code in your project, however, might assume that Python is being run in a subfolder. For example, suppose you open the root folder of the python_koans repository and then set the python3/contemplate-koans.py file as startup item. If you then run the code, you see an error that the koans.txt file cannot be found. This error happens because contemplate-koans.py assumes that Python is being run in the python3 folder rather than the repository root.

In such cases, you must also add a line to the launch configuration JSON file to specify the working directory:

  1. Right-click the Python (.py) startup file in Solution Explorer and select Debug and Launch Settings.

  2. In the Select debugger dialog box that appears, select Default and then choose Select.

    Note

    If you don't see Default as a choice, be sure that you chose a Python .py file when selecting the Debug and Launch Settings command. Visual Studio uses the file type to determine which debugger options to display.

  3. Visual Studio opens a file named launch.vs.json, which is located in the hidden .vs folder. This file describes the debugging context for the project. To specify a working directory, add a value for 'workingDirectory', as in 'workingDirectory': 'python3' for python-koans example:

  4. Save the file and launch the program again, which now runs in the specified folder.

Next steps

Visual Studio Code For Beginners

See also