Muokkaa

Jaa


Quickstart: Create Python project from existing code in Visual Studio

In this quickstart, you follow guided steps to quickly create a new Python project from existing code. Visual Studio makes it easy to bring your Python code into a Visual Studio project with the Create New Project from Existing Python Code wizard.

Prerequisites

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.

Use wizard to create project from existing files

Follow these steps to create a project from existing files.

Important

The following process doesn't move or copy any original source files. If you want to work with a copy of your files, first duplicate the folder and then create the project.

  1. Launch Visual Studio and select File > New > Project.

  2. In the Create a new project dialog, search for python, and select the From Existing Python code template, and select Next.

  3. In the Configure your new project dialog, enter a project Name and Location, choose the solution to contain the project, and select Create.

  4. In the Create New Project from Existing Python Code wizard, set the Folder path to your existing code, set a Filter for file types, and specify any Search paths that your project requires, then select Next. If you don't know the search paths, leave the field blank.

    Screenshot of a New Project creation from Existing Code, including choosing the folder path, file type filters, and search paths.

  5. On the next page, select the Startup file for your project. Visual Studio selects the default global Python interpreter and version. You can change the environment by using the dropdown menu. When you're ready, select Next.

    Note

    The dialog shows only files in the root folder. If the file you want is in a subfolder, leave the startup file blank. You can set the startup file in Solution Explorer, as described in a later step.

    Screenshot of New Project Creation from Existing Code window, including choosing the startup file and Python environment.

  6. Select the location to store the project file (a .pyproj file on disk). If applicable, you can also include autodetection of virtual environments and customize the project for different web frameworks. If you're unsure of these options, leave the fields set to the defaults.

    Screenshot of a New Project creation from Existing Code window, including the location for the project file and other optional settings.

  7. Select Finish.

    Visual Studio creates the project and opens it in Solution Explorer. If you want to move the .pyproj file to a different location, select the file in Solution Explorer, and then select File > Save As on the toolbar. This action updates file references in the project, but it doesn't move any code files.

  8. To set a different startup file, locate the file in Solution Explorer, right-click the file, and select Set as Startup File.

Now you can run your program by selecting Debug > Start without Debugging on the Visual Studio main toolbar or use the Ctrl+F5 keyboard shortcut.