How to run python code in visual studio code

In this article, I am going to guide you how to use VS Code (Visual Studio Code) to run python code. Either you are novice learner or experienced python developer you need an IDE(Integrated Development Environment) to run the code and build stuff.  There are many IDE already available for python but VS Code has become absolute favorite IDE  of many. I highly recommend those who want to write(or already writing) code in Python.

VSCode(Visual Studio Code), is a free and open source IDE(Integrated Development Environment) built for Windows, Mac OS X and Linux, from Microsoft. This IDE(Integrated Development Environment) is highly configurable and extensible with large various plugins, including Python.

Some of the reasons make us fall in love with Visual Studio Code are:

  • Visual Studio Code opens fast.
  • Visual Studio Code works on multiple operating systems (Linux/Windows/Mac)
  • Visual Studio Code includes useful features by default (keyboard column selection, indentation conversion, terminal, and so much more)
  • Visual Studio Code is the free and open source
  • Visual Studio Code is highly supported by Microsoft. Regular features enhancement and updated proves Microsoft is very much serious about open source platform.

Installation

Navigate here (https://code.visualstudio.com/#alt-downloads) to download an installer for Windows, Linux or Mac(Whatever platform you would like to use). In this article, I am using VS Code installer for windows.

You can make sure yourself that python interpreter successfully integrated with your environment variable path or not by using the command prompt. 

Required Extensions

Python – Linting, Debugging (multi-threaded, remote), Intellisense, code formatting, refactoring, unit tests.
Python for VSCode – Python language extension for vs code
MagicPython – Syntax highlighter for cutting-edge Python.
Python Extended – Python Extended is a vs code snippet that makes it easy to write codes in python by providing completion options along with all arguments.

Python Path and Version

By default, the extension uses the Python interpreter available in the current path. You could always change this by specifying the path to the interpreter in the user or workspace settings.

Select your preferred Python interpreter/version/environment using the command. Select Interpreter

  • By default, we use the one that’s on your path.
  • If you have a workspace open you can also click in the status bar to change the interpreter.

Python is an interpreted language, and in order to play with Python code and get Python IntelliSense, you must tell VS Code which interpreter have to use. select a Python interpreter by opening the Command Palette start typing the Python: Select Interpreter command to search. You can also use Shortcut (Ctrl+Shift+P) to open Command Palette.

If you select an interpreter without a workspace folder open, VS Code sets python.pythonPath in your user settings instead, which sets the default interpreter for VS Code in general. The user setting makes sure you always have a default interpreter for Python projects. The workspace settings let you override the user setting.

By using the file.py extension, VS Code interprets that this file is a Python and evaluates the contents with the Python extension and the selected interpreter.

For full details on editing, formatting, and refactoring, see Editing code. The Python extension also has full support for Linting.

Write and Run Simple Hello World Program in Python

Create a simple code with hello world message in HelloWorld.py and runcode with Python. Right-click in the editor and select Run Python File in Terminal(which saves the file automatically):

The command opens a terminal panel in which your Python interpreter is automatically activated, then runs python HelloWorld.py:

Debugging Hello World program

select the settings icon on the debug toolbar (or use the Debug > Open configurations menu command):

Just select Python: Current File (Integrated Terminal), which is the configuration that runs the current file shown in the editor using the currently selected Python interpreter.

To automatically stop the debugger on the first line when the program starts, add a "stopOnEntry": truesetting to the “Python: Current File” configuration in,launch.json so that the whole configuration appears as follows:

After doing little bit changes you have to save,launch.json and then you can switch to inHelloWorld.py the editor, and then run the debugger by selecting the green arrow in the Debug toolbar or pressing F5. Because stopOnEntry is set to true, the debugger stops on the first line of the file.

Read more for Python Debugging 

In case of any query while configuring Visual Studio Code for Python, please feel free to write to us.

Leave a Reply