How To Run Python Code In Visual Studio Code

Visual Studio Code (VS Code) has become a popular choice among developers for its lightweight yet powerful features. Among its many capabilities, it offers robust support for Python development, including features for creating, running, and debugging Python scripts. If you’re new to Python or Visual Studio Code, this guide will walk you through the essential steps to get started with Python development in VS Code.

1. Installing the Python Extension

The first step in setting up your Python development environment in Visual Studio Code is to install the Python extension. Here’s how you can do it:

Open Visual Studio Code. Click on the View menu at the top and select Extensions. In the Extensions panel, search for “python” using the search box.

Find the Python extension developed by Microsoft and click Install. Once installed, restart Visual Studio Code for the changes to take effect.

Before proceeding, ensure that you have Python installed on your operating system.

2. Selecting the Python Interpreter

After installing the Python extension, you need to select the Python interpreter you want to use in your projects. Follow these steps to do so:

Open Visual Studio Code. Click on View and then select Command Palette…. In the command palette, type “Python: Select Interpreter” and select it from the dropdown.

A list of installed Python interpreters will appear. Choose the one you want to use for your project.
visual studio code - command palette - show all python interpreters

3. Creating a Python Script File.

Create a folder in your OS, this folder will be treated as the workspace for your visual studio code python project.

Click File —> Open Folder… menu item in the visual studio code top menu bar. Browse the folder that you create and click the Open button in the folder browse dialog. Then it will list all the files in the folder on the visual studio code left side pane, the pane name is the uppercase of the folder name.

Click the New File button on the top right corner of the folder pane, or right-click the folder name and click the New File menu item on the popup menu list to create a new python file. You should make sure the file ends with a .py extension.
vscode-toolbar-new-file

Click the file name, it will open the file on the right side. Now you can write python code in the created python file like below.

msg = 'I love python'

print(msg)

4. Running Python Scripts.

Right-click the python file source code in the editor, it will popup a menu list. There are some menu items that can run the python file in different modes.

The Run Python File in Terminal menu item will start the visual studio code built-in terminal and run the python source code in the terminal.

When clicking this menu item, it will display the TERMINAL window at the bottom of the visual studio code. And run the python file in the terminal. You can see the output text like below in the TERMINAL window.

/usr/local/bin/python3 /Users/songzhao/Documents/WorkSpace/PythonExample/test.py

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
(base) songs-MacBook-Pro:PythonExample songzhao$ /usr/local/bin/python3 /Users/songzhao/Documents/WorkSpace/PythonExample/test.py
I love python

If you want to create a new TERMINAL window, you can click the menu item Terminal —> New Terminal / Split Terminal on the top menu bar.

If you click the Run Current File in Interactive Window menu item in the popup menu list when you right-click the python source file, it will open an interactive window on the right side of the visual studio code.

If you meet an error message such as ValueError: check_hostname requires server_hostname, you can read the article How To Fix ValueError: Check_hostname Requires Server_HostName to learn how to fix it.

There is a line of text Type ‘python’ code here and press Shift + Enter to run in a dark blue area at the interactive window bottom. You can input the python source code in the input text area and then press Shift + Enter key to run the python code.

You can see the python code executing result in the interactive window top area. For example, I input the below python source code in the interactive window input area.

for i in range(10):
    print(i)

When I press shift + enter keys at the same time, it will display both the python source code and result in the interactive window top area.

5. Debugging Python Source Code.

Single-click the first column of the python source code line to set a breakpoint. Click the visual studio code Run —> Start Debugging menu item at the top menu bar to start the debugging process.

Now it will open the command palette window, select the Python File Debug the current active Python file item in the drop-down list.
visual studio code debug python file configurations

The python program execution will stop at the first breakpoint. You can click the Continue, Step Over, Step Into, Step Out, Restart, Stop button in the debug toolbar to process the python code debugging.

visual studio code debug toolbar

6. Conclusion.

By following these steps, you can harness the power of Visual Studio Code for Python development, from writing code to running and debugging your scripts effectively. With its intuitive interface and extensive features, Visual Studio Code proves to be an excellent choice for Python developers of all levels.

7. Video Demo for This Article.

You can watch the video of this example article below.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.