Visual Studio Code Run Python



There isn’t much support for Python in Microsoft new code editor Visual Studio Code (VSCode), but there is a neat way to run your Python code right inside VSCode. It’s a feature called tasks and while the examples give are for compiling code, you can pretty much just run any program against the code you are editing in VSCode. Select one or more lines, then press Shift+Enter or right-click and select Run Selection/Line in Python Terminal. This command is convenient for testing just a part of a file. That is what is on the VS Code documentation for Python and is what works for me. It is Shift + Enter not Ctrl. Learn about Visual Studio Code as a Python IDE (code completion, debugging, snippets, linting).

Visual studio code run python script

You could install the Python extension for Visual Studio Code from the Visual Studio Code market place. Once done, use the 'Python Console' debug option to run and debug your Python code. This will launch the terminal/command window allowing you to capture input, and you wouldn't need to configure the tasks.json file for this. Python support is presently available only on Visual Studio for Windows; on Mac and Linux, Python support is available through Visual Studio Code. Download and run the latest Visual Studio installer for Windows (Python support is present in release 15.2 and later).

Visual Studio Code Run Python With Arguments

Visual studio code run python script-->

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

Visual Studio Code Run Python

Create Python Script In Visual Studio Code

Go deeper