Create a Dockerfile file describing a simple Python container. Build, run, and verify the functionality of a Django, Flask, or General Python app. Debug the app running in a container. After clicking to run the Python file in the terminal, Visual Studio Code will automatically write the path of the Python application and the Python file, again just like we did manually in the previous article. Now, everything is just happening inside the same application for our convenience! Working with Python in Visual Studio Code, using the Microsoft Python extension, is simple, fun, and productive. The extension makes VS Code an excellent Python editor, and works on any operating system with a variety of Python interpreters. Once discovered, Visual Studio Code provides a variety of means to run tests and debug tests. VS Code displays test output in the Python Test Log panel, including errors caused when a test framework is not installed. With pytest, failed tests also appear in the Problems panel. A little background on unit testing #.
-->Python is a popular programming language that is reliable, flexible, easy to learn, free to use on all operating systems, and supported by both a strong developer community and many free libraries. The language supports all manners of development, including web applications, web services, desktop apps, scripting, and scientific computing and is used by many universities, scientists, casual developers, and professional developers alike.
Visual Studio provides first-class language support for Python. This tutorial guides you through the following steps:
Prerequisites
- Visual Studio 2017 with the Python workload installed. For instructions, see Work with Python in Visual Studio - Step 0.
- Visual Studio 2019 with the Python workload installed. For instructions, see Work with Python in Visual Studio - Step 0.
You can also use an earlier version of Visual Studio with the Python Tools for Visual Studio installed. See Install Python support in Visual Studio.
Step 1: Create a new Python project
A project is how Visual Studio manages all the files that come together to produce a single application, including source code, resources, configurations, and so on. A project formalizes and maintains the relationship between all the project's files as well as external resources that are shared between multiple projects. As such, projects allow your application to effortlessly expand and grow much easier than simply managing a project's relationships in ad hoc folders, scripts, text files, and even your own mind.
In this tutorial you begin with a simple project containing a single, empty code file.
In Visual Studio, select File > New > Project (Ctrl+Shift+N), which brings up the New Project dialog. Here you browse templates across different languages, then select one for your project and specify where Visual Studio places files.
To view Python templates, select Installed > Python on the left, or search for 'Python'. Using search is a great way to find a template when you can't remember its location in the languages tree.
Notice how Python support in Visual Studio includes a number of project templates, including web applications using the Bottle, Flask, and Django frameworks. For the purposes of this walkthrough, however, let's start with an empty project.
Select the Python Application template, specify a name for the project, and select OK.
After a few moments, Visual Studio shows the project structure in the Solution Explorer window (1). The default code file is open in the editor (2). The Properties window (3) also appears to show additional information for any item selected in Solution Explorer, including its exact location on disk.
Take a few moments to familiarize yourself with Solution Explorer, which is where you browse files and folders in your project.
(1) Highlighted in bold is your project, using the name you gave in the New Project dialog. On disk, this project is represented by a .pyproj file in your project folder.
(2) At the top level is a solution, which by default has the same name as your project. A solution, represented by a .sln file on disk, is a container for one or more related projects. For example, if you write a C++ extension for your Python application, that C++ project could reside within the same solution. The solution might also contain a project for a web service, along with projects for dedicated test programs.
(3) Under your project you see source files, in this case only a single .py file. Selecting a file displays its properties in the Properties window. Double-clicking a file opens it in whatever way is appropriate for that file.
(4) Also under the project is the Python Environments node. When expanded, you see the Python interpreters that are available to you. Expand an interpreter node to see the libraries that are installed into that environment (5).
Right-click any node or item in Solution Explorer to access a menu of applicable commands. For example, the Rename command allows you to change the name of any node or item, including the project and the solution.
Next step
Go deeper
- Python projects in Visual Studio.
- Python for Beginners (python.org)
A project is how Visual Studio manages all the files that come together to produce a single application, including source code, resources, configurations, and so on. A project formalizes and maintains the relationship between all the project's files as well as external resources that are shared between multiple projects. As such, projects allow your application to effortlessly expand and grow much easier than simply managing a project's relationships in ad hoc folders, scripts, text files, and even your own mind.
In this tutorial you begin with a simple project containing a single, empty code file.
In Visual Studio, select File > New > Project (Ctrl+Shift+N), which brings up the New Project dialog. Here you browse templates across different languages, then select one for your project and specify where Visual Studio places files.
To view Python templates, select Installed > Python on the left, or search for 'Python'. Using search is a great way to find a template when you can't remember its location in the languages tree.
Notice how Python support in Visual Studio includes a number of project templates, including web applications using the Bottle, Flask, and Django frameworks. For the purposes of this walkthrough, however, let's start with an empty project.
Select the Python Application template, specify a name for the project, and select OK.
After a few moments, Visual Studio shows the project structure in the Solution Explorer window (1). The default code file is open in the editor (2). The Properties window (3) also appears to show additional information for any item selected in Solution Explorer, including its exact location on disk.
Take a few moments to familiarize yourself with Solution Explorer, which is where you browse files and folders in your project.
(1) Highlighted in bold is your project, using the name you gave in the New Project dialog. On disk, this project is represented by a .pyproj file in your project folder.
(2) At the top level is a solution, which by default has the same name as your project. A solution, represented by a .sln file on disk, is a container for one or more related projects. For example, if you write a C++ extension for your Python application, that C++ project could reside within the same solution. The solution might also contain a project for a web service, along with projects for dedicated test programs.
(3) Under your project you see source files, in this case only a single .py file. Selecting a file displays its properties in the Properties window. Double-clicking a file opens it in whatever way is appropriate for that file.
(4) Also under the project is the Python Environments node. When expanded, you see the Python interpreters that are available to you. Expand an interpreter node to see the libraries that are installed into that environment (5).
Right-click any node or item in Solution Explorer to access a menu of applicable commands. For example, the Rename command allows you to change the name of any node or item, including the project and the solution.
Next step
Go deeper
- Python projects in Visual Studio.
- Python for Beginners (python.org)
Visual Studio Code Python Tutorial Beginners
-->Previous step: Create a new Python project
Visual Studio Run Python Script
Although Solution Explorer is where you manage project files, the editor window is typically where you work with the contents of files, like source code. The editor is contextually aware of the type of file you're editing, including the programming language (based on the file extension), and offers features appropriate to that language such as syntax coloring and auto-completion using IntelliSense.
After creating a new 'Python Application' project, a default empty file named PythonApplication1.py is open in the Visual Studio editor.
In the editor, start typing
print('Hello, Visual Studio')
and notice how Visual Studio IntelliSense displays auto-completion options along the way. The outlined option in the drop-down list is the default completion that's used when you press the Tab key. Completions are most helpful when longer statements or identifiers are involved.IntelliSense shows different information depending on the statement you're using, the function you're calling, and so forth. With the
print
function, typing(
afterprint
to indicate a function call displays full usage information for that function. The IntelliSense pop up also shows the current argument in boldface (value as shown here):Complete the statement so it matches the following:
Notice the syntax coloration that differentiates the statement
print
from the argument'Hello Visual Studio'
. Also, temporarily delete the last'
on the string and notice how Visual Studio shows a red underline for code that contains syntax errors. Then replace the'
to correct the code.Tip
Because one's development environment is a very personal matter, Visual Studio gives you complete control over Visual Studio's appearance and behavior. Select the Tools > Options menu command and explore the settings under the Environment and Text Editor tabs. By default you see only a limited number of options; to see every option for every programming language, select Show all settings at the bottom of the dialog box.
Run the code you've written to this point by pressing Ctrl+F5 or selecting Debug > Start without Debugging menu item. Visual Studio warns you if you still have errors in your code.
When you run the program, a console window appears displaying the results, just as if you'd run a Python interpreter with PythonApplication1.py from the command line. Press a key to close the window and return to the Visual Studio editor.
In addition to completions for statements and functions, IntelliSense provide completions for Python
import
andfrom
statements. These completions help you easily discover what modules are available in your environment and the members of those modules. In the editor, delete theprint
line and start typingimport
. A list of modules appears when you type the space:Complete the line by typing or selecting
sys
.On the next line, type
from
to again see a list of modules:Select or type
math
, then continue typing with a space andimport
, which displays the module members:Finish by importing the
sin
,cos
, andradians
members, noticing the auto-completions available for each. When you're done, your code should appear as follows:Tip
Completions work with substrings as you type, matching parts of words, letters at the beginning of words, and even skipped characters. See Edit code - Completions for details.
Add a little more code to print the cosine values for 360 degrees:
Run the program again with Ctrl+F5 or Debug > Start without Debugging. Close the output window when you're done.