

If you want to keep the output window open for any reason, right-click your project, select Properties, select the Debug tab, then add -i to the Interpreter Arguments field. If you encounter any of these behaviors, check that you have an assigned startup file. In this case, Visual Studio 2017 version 15.6 and later shows an error earlier versions either open an output window with the Python interpreter running, or you see the output window appear but then disappear almost immediately. If you remove the selected startup file from a project and don't select a new one, Visual Studio doesn't know what Python file to start with when you try to run the project. To change it, right-click the new file and select Set as Startup Item (or Set as Startup File in older versions of Visual Studio). The startup file is the file that's run when you start debugging ( F5 or Debug > Start Debugging) or when you run your project in the Interactive window ( Shift+ Alt+ F5 or Debug > Execute Project in Python Interactive). You can explore these options with a test project to learn what's available in your version of Visual Studio.Įach Python project has one assigned start-up file, shown in boldface in Solution Explorer. As described on the item templates reference, options include empty Python files, a Python class, a unit test, and various files related to web applications. Adding such files is done by right-clicking the project and selecting Add > Existing Item with which you browse for a file to add, or Add > New Item, which brings up a dialog with a variety of item templates. Add files, assign a startup file, and set environmentsĪs you develop your application, you typically need to add new files of different types to the project. It's a simple matter to create a Visual Studio project from existing code, as described in Create a project from existing files. Furthermore, Visual Studio analyzes all files and packages in the folder from which the single file is opened, which could consume considerable CPU time.
#Create simple python in visual studio code#
Because such code always uses the default global environment, however, you may see incorrect completions or errors if the code is meant for a different environment. For example, you can open a Python file by itself and enjoy auto-complete, IntelliSense, and debugging (by right-clicking in the editor and selecting Start with Debugging). Without a project, all versions of Visual Studio work well with Python code. There are, however, benefits to using a project file, as explained in this section. For more information, see Quickstart: Open and run Python code in a folder. Visual Studio 2019 supports opening a folder containing Python code and running that code without creating Visual Studio project and solution files. Visual Studio provides a variety of Python project templates to quickly set up a number of application structures, including a template to create a project from an existing folder tree and a template to create a clean, empty project. (For a general discussion, see Solutions and projects in Visual Studio.) With this relationship, Visual Studio automatically builds the C++ project (if necessary) when you start debugging the Python project. For example, a Python project can reference a C++ project that implements an extension module. In addition, projects are always managed within a Visual Studio solution, which can contain any number of projects that might reference one another. pyproj file) identifies all the source and content files associated with your project, contains build information for each file, maintains the information to integrate with source-control systems, and helps you organize your application into logical components.

A Visual Studio project helps manage this complexity. Python applications are typically defined using only folders and files, but this structure can become complex as applications become larger and perhaps involve auto-generated files, JavaScript for web applications, and so on.
