Use Visual Studio Code with Python

As an Amazon Associate I earn from qualifying purchases.

6 Min Read

Today we will be talking about how to use Visual Studio Code with Python. Visual studio code is an extremely versatile tool when it comes to development. More and more, I find myself using VS Code due to the number of languages it supports and also due to the strong community it has. Let’s have a look at how we can install VS code and use it for Python.

Installing VS Code

Go to the downloads page for VS Code and download the specific version for your OS. You can download versions for Windows, macOS and Linux. During the install, leave all settings as default and install the application. If successful, you will see the following screen.

Go ahead and click finish to launch VS Code.

Opening a Python project in VS Code

Once VS code is installed, you will want to open up a Python project. Do this by clicking File->Open Folder

Navigate to the root of your Python project and click ‘Select Folder’

Installing Python Extension in VS Code

Now that you have a Python project opened in VS Code, you should go ahead and install the recommended Python extension. Installing this extension will include features such as IntelliSense, linting, debugging, code navigation, code formatting, Jupyter notebook support, refactoring, variable explorer, test explorer, and more! You can read more about the extension here.

On the left hand side of VS code you should see an icon in the left toolbar pictured below:

Click this icon and search for Python in the ‘Search Extensions in Marketplace’ search box:

Click on the first result for Python and click the install button in the middle of the page.

Common Errors

Once installed you may run into the following issue:

This means that VS Code was unable to find your Python installation. To fix this, click ‘Select Python Interpreter’. Once clicked you will see another popup in the top middle of the screen:

If VS Code finds your Python installation click on it. Otherwise you will need to click ‘Enter interpreter path’ and navigate to your Python installation.

Launching a Python script in VS Code

By this stage you should have VS Code installed, your Python projected loaded in and the Python extension for VS Code installed. Since we want to use Visual Studio Code with Python, let’s have a look at how we can launch a Python script.

The easiest way to run a Python script is to click on it from the explorer on the left side of the page, click ‘Run’ on the toolbar at the top and click ‘Run Without Debugging’

Similarly, you can also run the same script in debug mode by clicking ‘Start Debugging’.

Launch configurations (launch.json) in VS Code

Another way to run a Python project is to create a launch configuration. This will allow you to customize the script being run (e.g. passing command-line arguments). To get started click on the following icon on the left toolbar and then click ‘create a launch.json file’

Once you have clicked this, you will see another popup in the top middle of the screen. Click the ‘Python File’ option.

You will notice in the top left of the screen there will now be a play button with the current launch configuration “Python: Current File”. This is created by launch.json. If you click the play button, it will execute the currently opened file.

Let’s say we have a file called main.py and regardless of what file we are viewing we would always like to execute main.py. To do this, let’s add another configuration to launch.json

Adding a new Configuration

Your configuration in launch.json should look like the following:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

Start by adding a comma after the last config followed by 2 opening and closing braces:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {

        }
    ]
}

Following the convention of the initial configuration, copy ‘name’, ‘type’, ‘request’, ‘program’ and ‘console’ into your new configuration:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },

        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

Now let’s give this configuration different parameter values. For name, I will change this to ‘Run main.py’ since that is the script I want to run. However, this name can be anything you like.

The only other parameter you need to change is program. Change this to “main.py”:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "name": "Run main.py",
            "type": "python",
            "request": "launch",
            "program": "main.py",
            "console": "integratedTerminal"
        }
    ]
}

Now, if you click the drop-down beside “Run” on the top left of the screen you should be able to see both run configurations.

Adding an argument to launch.json

Adding command line arguments to your launch configuration is easy in Visual Studio Code. Add a new key named args and assign it to an array of arguments like below:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "name": "Run main.py'",
            "type": "python",
            "request": "launch",
            "program": "main.py",
            "args": ["firstArg", "secondArg"],
            "console": "integratedTerminal"
        }
    ]
}

When my script is executed, you can see both arguments printed to the terminal:

A full list of arguments for launch.json can be found at https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes.

That’s all for how to Use Visual Studio Code with Python!

If you’re interested in learning Python I highly recommend this book. In the first half of the book, you”ll learn basic programming concepts, such as variables, lists, classes, and loops, and practice writing clean code with exercises for each topic. In the second half, you”ll put your new knowledge into practice with three substantial projects: a Space Invaders-inspired arcade game, a set of data visualizations with Python”s handy libraries, and a simple web app you can deploy online. Get it here.

Interested in learning Python? Check out my other tutorials here: https://www.conorjohanlon.com/category/python-tutorials/

As always, if you have any questions or comments please feel free to post them below. Additionally, if you run into any issues please let me know.

Leave a Comment

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

Subscribe to my newsletter to keep up to date with my latest posts

Holler Box