Run Python Code In Visual Studio

  



  1. Visual Studio Code Tutorials
  2. Run Python Script In Visual Studio Community

To run code: use shortcut Ctrl+Alt+N; or press F1 and then select/type Run Code, or right click the Text Editor and then click Run Code in editor context menu; or click Run Code button in editor title menu; or click Run Code button in context menu of file explorer; To stop the running code: use shortcut Ctrl+Alt+M; or press F1 and then select.

  1. To run Python code: use shortcut Ctrl + Alt + N or press F1 and then select/type Run Code, or right click the Text Editor and then click Run Code in the editor context menu.
  2. Run Python program in Visual studio code on windows OSHi, Guys In this video I'm going to explainHow to download Python IDEL and its installingHow to downl.
  3. Visual Studio also provides tight integration between the Python code editor and the Interactive window. The Ctrl + Enter keyboard shortcut conveniently sends the current line of code (or code block) in the editor to the Interactive window, then moves to the next line (or block).
-->

Previous step: Create a new Python project

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.

  1. After creating a new 'Python Application' project, a default empty file named PythonApplication1.py is open in the Visual Studio editor.

  2. 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.

  3. IntelliSense shows different information depending on the statement you're using, the function you're calling, and so forth. With the print function, typing ( after print 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):

  4. Complete the statement so it matches the following:

  5. 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.

  6. 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.

  7. 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.

  8. In addition to completions for statements and functions, IntelliSense provide completions for Python import and from statements. These completions help you easily discover what modules are available in your environment and the members of those modules. In the editor, delete the print line and start typing import. A list of modules appears when you type the space:

  9. Complete the line by typing or selecting sys.

  10. On the next line, type from to again see a list of modules:

  11. Select or type math, then continue typing with a space and import, which displays the module members:

  12. Finish by importing the sin, cos, and radians 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.

  13. Add a little more code to print the cosine values for 360 degrees:

  14. Run the program again with Ctrl+F5 or Debug > Start without Debugging. Close the output window when you're done.

Next step

Go deeper

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. It leverages all of VS Code's power to provide auto complete and IntelliSense, linting, debugging, and unit testing, along with the ability to easily switch between Python environments, including virtual and conda environments.

This article provides only an overview of the different capabilities of the Python extension for VS Code. For a walkthrough of editing, running, and debugging code, use the button below.

Install Python and the Python extension

The tutorial guides you through installing Python and using the extension. You must install a Python interpreter yourself separately from the extension. For a quick install, use Python 3.7 from python.org and install the extension from the VS Code Marketplace.

Once you have a version of Python installed, activate it using the Python: Select Interpreter command. If VS Code doesn't automatically locate the interpreter you're looking for, refer to Environments - Manually specify an interpreter.

You can configure the Python extension through settings. Learn more in the Python Settings reference.

Windows Subsystem for Linux: If you are on Windows, WSL is a great way to do Python development. You can run Linux distributions on Windows and Python is often already installed. When coupled with the Remote - WSL extension, you get full VS Code editing and debugging support while running in the context of WSL. To learn more, go to Developing in WSL or try the Working in WSL tutorial.

Insiders program

The Insiders program allows you to try out and automatically install new versions of the Python extension prior to release, including new features and fixes.

If you'd like to opt into the program, you can either open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and select Python: Switch to Insiders Daily/Weekly Channel or else you can open settings (⌘, (Windows, Linux Ctrl+,)) and look for Python: Insiders Channel to set the channel to 'daily' or 'weekly'.

Run Python code

To experience Python, create a file (using the File Explorer) named hello.py and paste in the following code (assuming Python 3):

The Python extension then provides shortcuts to run Python code in the currently selected interpreter (Python: Select Interpreter in the Command Palette):

  • In the text editor: right-click anywhere in the editor and select Run Python File in Terminal. If invoked on a selection, only that selection is run.
  • In Explorer: right-click a Python file and select Run Python File in Terminal.

You can also use the Terminal: Create New Integrated Terminal command to create a terminal in which VS Code automatically activates the currently selected interpreter. See Environments below. The Python: Start REPL activates a terminal with the currently selected interpreter and then runs the Python REPL.

For a more specific walkthrough on running code, see the tutorial.

Autocomplete and IntelliSense

The Python extension supports code completion and IntelliSense using the currently selected interpreter. IntelliSense is a general term for a number of features, including intelligent code completion (in-context method and variable suggestions) across all your files and for built-in and third-party modules.

Visual Studio Code Tutorials

IntelliSense quickly shows methods, class members, and documentation as you type, and you can trigger completions at any time with ⌃Space (Windows, Linux Ctrl+Space). You can also hover over identifiers for more information about them.

Tip: Check out the IntelliCode extension for VS Code (preview). IntelliCode provides a set of AI-assisted capabilities for IntelliSense in Python, such as inferring the most relevant auto-completions based on the current code context.

Linting

Python

Linting analyzes your Python code for potential errors, making it easy to navigate to and correct different problems.

The Python extension can apply a number of different linters including Pylint, pycodestyle, Flake8, mypy, pydocstyle, prospector, and pylama. See Linting.

Debugging

No more print statement debugging! Set breakpoints, inspect data, and use the debug console as you run your program step by step. Debug a number of different types of Python applications, including multi-threaded, web, and remote applications.

For Python-specific details, including setting up your launch.json configuration and remote debugging, see Debugging. General VS Code debugging information is found in the debugging document. The Django and Flask tutorials also demonstrate debugging in the context of those web apps, including debugging Django page templates.

Environments

The Python extension automatically detects Python interpreters that are installed in standard locations. It also detects conda environments as well as virtual environments in the workspace folder. See Configuring Python environments. You can also use the python.pythonPath setting to point to an interpreter anywhere on your computer.

The current environment is shown on the left side of the VS Code Status Bar:

The Status Bar also indicates if no interpreter is selected:

Run Python Script In Visual Studio Community

The selected environment is used for IntelliSense, auto-completions, linting, formatting, and any other language-related feature other than debugging. It is also activated when you use run Python in a terminal.

To change the current interpreter, which includes switching to conda or virtual environments, select the interpreter name on the Status Bar or use the Python: Select Interpreter command.

VS Code prompts you with a list of detected environments as well as any you've added manually to your user settings (see Configuring Python environments).

Installing packages

Packages are installed using the Terminal panel and commands like pip install <package_name> (Windows) and pip3 install <package_name> (macOS/Linux). VS Code installs that package into your project along with its dependencies. Examples are given in the Python tutorial as well as the Django and Flask tutorials.

Jupyter notebooks

If you open a Jupyter notebook file (.ipynb) in VS Code, you can use the Jupyter Notebook Editor to directly view, modify, and run code cells.

You can also convert and open the notebook as a Python code file. The notebook's cells are delimited in the Python file with #%% comments, and the Python extension shows Run Cell or Run All Cells CodeLens. Selecting either CodeLens starts the Jupyter server and runs the cell(s) in the Python interactive window:

Opening a notebook as a Python file allows you to use all of VS Code's debugging capabilities. You can then save the notebook file and open it again as a notebook in the Notebook Editor, Jupyter, or even upload it to a service like Azure Notebooks.

Using either method, Notebook Editor or a Python file, you can also connect to a remote Jupyter server for running the code. For more information, see Jupyter support.

Testing

The Python extension supports testing with the unittest, pytest, and nose test frameworks.

To run tests, you enable one of the frameworks in settings. Each framework also has specific settings, such as arguments that identify paths and patterns for test discovery.

Once discovered, VS Code provides a variety of commands (on the Status Bar, the Command Palette, and elsewhere) to run and debug tests, including the ability to run individual test files and individual methods.

Configuration

The Python extension provides a wide variety of settings for its various features. These are described on their relevant topics, such as Editing code, Linting, Debugging, and Testing. The complete list is found in the Settings reference.

Other popular Python extensions

The Microsoft Python extension provides all of the features described previously in this article. Additional Python language support can be added to VS Code by installing other popular Python extensions.

  1. Open the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)).
  2. Filter the extension list by typing 'python'.

The extensions shown above are dynamically queried. Click on an extension tile above to read the description and reviews to decide which extension is best for you. See more in the Marketplace.

Next steps

  • Python Hello World tutorial - Get started with Python in VS Code.
  • Editing Python - Learn about auto-completion, formatting, and refactoring for Python.
  • Basic Editing - Learn about the powerful VS Code editor.
  • Code Navigation - Move quickly through your source code.
Studio
03/07/2019