Bewerken

Delen via


Quickstart: Open and run Python code in a folder in Visual Studio

In this quickstart, you follow guided steps to run Python code in Visual Studio 2019 and later without having to create a Visual Studio project. Visual Studio makes it easy to open and run existing Python code from a folder. The same features and commands are available for your Python code development as when you choose to work with a project.

Prerequisites

  • Visual Studio installed with support for Python workloads. For more information, see Install Python support in Visual Studio.

  • The instructions in this quickstart apply to any folder with Python code. To follow along with the example described in this article, clone the gregmalcolm/python_koans GitHub repository to your computer by using the following command:

    git clone https://github.com/gregmalcolm/python_koans
    

Visual Studio for Mac isn't supported. For more information, see What's happening to Visual Studio for Mac? Visual Studio Code on Windows, Mac, and Linux works well with Python through available extensions.

Open the local code folder

Follow these steps to open a local folder with existing Python code in Visual Studio:

  1. Launch Visual Studio. In the start window, select Open a local folder in the Get started column:

    Screenshot that shows how to select the Open a local folder option when Visual Studio starts.

    If you're using the python_koans repository example code, make sure to select the python3 folder within the clone folder.

    If Visual Studio is already running, you can select File > Open > Folder instead.

  2. Browse to the folder that contains your Python code and choose Select Folder:

  3. Visual Studio displays the files in Solution Explorer in the Folder View. You can expand and collapse a folder by using the arrow to the left of the folder name:

  4. When you open a Python folder, Visual Studio creates several hidden folders to manage settings related to the program. To see these folders (and any other hidden files and folders, such as the .git folder), select the Show All Files toolbar option:

Run the program

After you open the existing Python code in Visual Studio, you can run the program. To run the code, you need to identify the Startup File (Startup Item) or primary program file for Visual Studio to execute the program. In this example, the startup file is contemplate-koans.py.

  1. In Solution Explorer, right-click the contemplate-koans.py file and select Set as Startup Item:

    Important

    If your Startup Item isn't located in the root of the folder you opened, you must also add a line to the launch configuration JSON file as described in Set working directory.

  2. Run the code by selecting Debug > Start without Debugging or use the keyboard shortcut Ctrl+F5. You can also select the solid play arrow next to the Startup Item name on the Visual Studio toolbar. This option runs the code in the Visual Studio Debugger.

    For all of these start methods, Visual Studio detects that your Startup Item is a Python file and automatically runs the code in the default Python environment. The current environment is shown to the right of the Startup Item name on the toolbar. In the following example, the current environment is Python 3.11 (64-bit):

    Screenshot that shows how to start the program with debugging by using the solid play arrow on the Visual Studio 2022 toolbar.

    For all of these start methods, Visual Studio detects that your Startup Item is a Python file and automatically runs the code in the default Python environment. The current environment is shown to the right of the Startup Item name on the toolbar. In the following example, the current environment is Python 3.6 (32-bit):

    Screenshot that shows how to start the program with debugging by using the solid play arrow on the Visual Studio.

    If you don't see the current Python Environment on the toolbar, select View > Other windows > Python Environments.

  3. When the program runs, Visual Studio opens a command window to display the program output:

    Note

    If you run the python-koans program with debugging, you need to change values in the code for the program to complete execution.

  4. You can run the code in a different Python environment:

    1. Expand the current Python Environment dropdown list in the Visual Studio toolbar and select the desired environment.

    2. Restart your program.

  5. When you're ready to close the code folder in Visual Studio, select File > Close folder.

Set working directory

By default, Visual Studio runs a Python project opened as a folder in the root of that same folder. However, the code in your project might expect Python to run in a subfolder. When your code expects to find files in different locations than the default configuration recognized by Visual Studio, you can experience errors when you try to run the code.

Suppose you open the root folder of the python_koans repository and see a subfolder named python3 that contains a Python file named contemplate-koans.py. You decide to set the python3/contemplate-koans.py file as the Startup File. When you run the code, you see an error that a file named koans.txt can't be found. The error occurs because the contemplate-koans.py file expects Python to 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. In Solution Explorer, right-click the Python (.py) Startup File and select Add Debug Configuration:

    Screenshot that shows how to select the Add Debug Configuration option for a selected file in Solution Explorer in Visual Studio 2022.

  2. In the Select Debugger dialog, select the Default option in the list, and then choose Select:

    Screenshot that shows how to select the Default Debugger in the Select a Debugger dialog in Visual Studio 2022.

    Note

    If you don't see Default as an option, be sure to choose a Python .py file when you select the Add Debug Configuration 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 the "workingDirectory" property. For the python-koans example, you can add the property and value, "workingDirectory": "python3":

    {
      "version": "0.2.1",
      "defaults": {},
      "configurations": [
        {
          "type": "python",
          "interpreter": "(default)",
          "interpreterArguments": "",
          "scriptArguments": "",
          "env": {},
          "nativeDebug": false,
          "webBrowserUrl": "",
          "project": "contemplate_koans.py",
          "projectTarget": "",
          "name": "contemplate_koans.py",
          "workingDirectory": "python3"
        }
      ]
    }
    
  4. Save your changes to the launch.vs.json file.

  5. Run your program again. The code execution should now run in the specified folder.

  1. In Solution Explorer, right-click the Python (.py) Startup File and select Debug and Launch Settings:

    Screenshot that shows how to select the Debug and Launch Settings option for a selected file in Solution Explorer in Visual Studio.

  2. In the Select Debugger dialog, select the Default option in the list, and then choose Select:

    Screenshot that shows how to select the Default Debugger in the Select a Debugger dialog in Visual Studio.

    Note

    If you don't see Default as an option, be sure to choose a Python .py file when you select 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 the "workingDirectory" property. For the python-koans example, you can add the property and value, "workingDirectory": "python3":

    {
      "version": "0.2.1",
      "defaults": {},
      "configurations": [
        {
          "type": "python",
          "interpreter": "(default)",
          "interpreterArguments": "",
          "scriptArguments": "",
          "env": {},
          "nativeDebug": false,
          "webBrowserUrl": "",
          "project": "contemplate_koans.py",
          "projectTarget": "",
          "name": "contemplate_koans.py",
          "workingDirectory": "python3"
        }
      ]
    }
    
  4. Save your changes to the launch.vs.json file.

  5. Run your program again. The code execution should now run in the specified folder.