How to Run Python Code in Visual Studio Code

Python is a general-purpose programming language. It’s easy to learn and use also has libraries that allow you to build software, apps, and websites. This article shows you how to get started with Python development in Visual Studio Code.

Step 1: Install the tools required

Verify The Python Installation

Before installing Python, we should check if we have Python already installed. If you’re on Windows, open a command prompt window and type

py —version

Then press Enter.

Verifying Python Installation
Type these commands in the command prompt to verify Python installation.

On Mac or Linux, use the same command to verify Python installation in the console.

Download and install Python

If you don’t have Python installed, follow these steps:

  1. Go to python.org/downloads. Click on the download button to begin downloading the latest Python version.

    Download Python
    Go to python.org/downloads to download Python.

  2. When given the option, choose to run the installer. After the installer starts, be sure to enable Add Python to PATH. Then run the download and select Install Now. There’s no need to further customize the installation.

    Python Installer
    Click on Install Now to run the Python installation.

Install Python extension in VS Code

While you could run Python programs using the default installation of Visual Studio Code, the Python Extension makes it much easier. This extension saves a lot of time when you have to run the program multiple times. Follow these steps to install it:

  1. Launch VS Code. From the sidebar, select Extensions. This will open an extensions panel.

    Extensions Pane
    Click on Extensions from the sidebar on the left side.

  2. Search for “Python” in the search box and click on Install.

    Search and Install Python Extension
    Look for Python, then select Install.

  3. After installation, reload/ restart Visual Studio Code.

Step 2: Create a Python program

With Python installed in your machine, you’re ready to write basic Python code in Visual Studio Code for testing. If you already have a Python program ready to be run, skip to Step 3. Otherwise, follow the below steps:

  1. Create a folder to store your Python programs. Click Open folder.

    Open Folder
    Click on Open folder button to start creating a folder.

  2. Create a folder in the preferred directory. I’m going to call it “Test.”

    Choose directory
    Choose your preferred directory for your folder.

  3. You’ll see a project explorer view for your project, which shows programs or directories for your project.We can create a Python program in this folder by pressing the New File button next to your folder heading. I will call it “hello.py”, as it will print out the words “Hello World.” Note that I put .py, which is the extension for python files.

    Create File
    Click on New File to start writing your Python program.

  4. Save the file.

Step 3: Run the Python program in VS Code

You can run the python program in the terminal through the default code or by using shortcuts supported by the Python Extension.

Default method

  1. Select New Terminal from the Terminal tab or use the shortcut CTRL ~ to run this program. It will open a terminal panel below the code editor area.

    New Terminal
    From the Terminal tab, select New Terminal.

  2. To launch your python file in the terminal, type python followed by the name of the python file. For the example we created in Step 2, type:
    Python hello.py

    Then hit Enter.

    Running Python On Terminal
    Write Python followed by the filename to run the program.

    It will run the python file. As you see, I successfully ran the program.

Python Extension Shortcut

The problem with the above method is that we’ll have to retype the code each time we want to run it. This is where Python Extension is useful. The extension will add a Run button in the top-right.

Run Button
Python Extension will enable the Run button.

Clicking on the Run button it will run the python file directly within the integrated terminal in Visual Studio Code.

Output using Python Extension
Click on the Run button and it will fetch you the result.

You’re now ready to write and run python programs in Visual Studio Code! Installing Python Extension streamlines the running process – try to explore some of the other benefits provided by the extension.

Join the conversation

Leave a Comment