4 Ways to Run Python Code in Visual Studio Code

Microsoft’s Visual Studio Code (VSC, or Code, for short) has quickly become one of the most popular code editors (i.e., integrated development environment, or IDE) because of its cross-cross platform capabilities, high level of customizable options, integrations for nearly all programming languages, and cost (it’s free!). These features persuaded me to drop my previous IDEs and I’ve been using VSC almost exclusively for the last three years.

While Code has a lot of good things going for it (more than most free IDEs) there can be some challenges, especially if you’re using Code for the first time. One of the biggest problems I had with Code was learning how to run the Python code I was writing. This was a major sticking point because I often use various Python distributions (both Anaconda/Miniconda distributions and default Python distributions) and I had trouble consistently running my Python scripts with Visual Studio Code.

If you’re having the same problem, this article is for you. Over the last few years, I’ve identified 4 reliable methods to run Python code in VSC. If one of these methods doesn’t work or seems too complicated, don’t worry there are three others you can try!

Before you get started with these steps, you may want to follow my FREE course for setting up Python and VSC.

1. From an external command prompt

If you want a method to run code in VSC that is guaranteed to work it’s using VSC to write and edit your scripts and then running the scripts from an external command prompt. Unfortunately, this can also be somewhat inconvenient because you have to open the proper terminal, activate a python environment, then type a command in the terminal whenever you want to run a script.

Nevertheless, it’s simple and the easiest way to get started if you don’t want to spend time figuring out settings inside of Code. To this day I usually open an external command prompt if I’m running a conda-based Python distribution. It’s just what I’m used to, and there’s more room in the code editor to navigate the code without the terminal window pulled up.

All you need to do this is have a terminal where the python distribution you want to use is set up as the default (so when you start an interactive Python session by typing python you get the version and distribution you expect). Open the terminal type python path/to/script.py, hit enter, and your code will run. Output from your script will appear in the external terminal, and you’ll just VSC as a glorified text editor.

This method is exceedingly reliable, but it can be a little inconvenient.

2. Use ‘Run Python File in Terminal’

The most convenient way to run your Python code is to have a button you can click and watch your code run inside of the IDE. In Code, this is possible by right-clicking on a file in the File Explorer (not from the Open Editors section) and selecting ‘Run Python file in Terminal’ (see image below).

For this to work, you’ll first need to specify the Python environment you are using. In Visual Studio Code you and select your Python environment in a few ways.

First, you may see a Python environment name and version number (or a message indicating no environment/interpreter is selected) on the blue ribbon in the bottom right corner of the IDE. You can click on this to select a new interpreter.

Second, if you don’t see the Python version in the blue ribbon, you can use Ctrl+Shift+P to open the settings palette. Here you can search for ‘Select Python Interpreter’. By default, VSC should recognize the Python environments available on your system. If it does not, there is an option to manually add an environment.

Once you have the proper environment set, right-click on the file you want to run (remember in the File Explorer, not Open Editors) and select ‘Run Python File in Terminal’. This command will open a terminal inside of the Visual Studio Code IDE, activate the Python environment you’ve selected, and run the Python script.

This method does require a little bit of setup, but I’ve found it to be very reliable once the setup is done properly. It’s also much more convenient to be able to run your code directly inside of the IDE without typing commands and managing a separate terminal.

How to access ‘Run Python File in Terminal’ from the Visual Studio Code main window.

If you’re having trouble getting this method to work, then you may want to take a look at my instructions for updating the VSC settings to define your default interpreter.

3. With the ‘Run Python File’ Button

If you’ve tried to run Python code in Visual Studio Code and failed, it’s likely because you were using the ‘Run Python File’ button. This is the button in the top-right corner of the IDE that looks like the ‘play’ symbol (see image below). There’s no need to be embarrassed. I’ve tried this unsuccessfully many times myself.

To use the ‘Run Python File’ button you need to have a Python environment and terminal that recognizes that Python distribution set up in VSC. This may sound complicated, but it’s not.

If you’ve successfully run a Python script using ‘Run Python File in Terminal’ (method #2, listed above) then everything should be set up and you can now use the ‘Run Python File’ button.

If you’re starting from scratch, the best option is to follow the directions above for method #2. If those directions don’t work for you, then try the directions in this article that will walk you through setting an environment and terminal.

The ‘Run Python File’ button in Visual Studio Code.

4. In a Jupyter Notebook

You can create and run Jupyter notebooks right inside of VSC. It’s super easy.

You’ll just need to install a couple of Visual Studio Code extensions that enable rendering of the notebook (free and easy), then create a Python environment and install the Jupyter module. I have a set of step-by-step instructions you can follow to get things set up!

Then all you have to do is create a new file in the file explorer (usually in the left panel) and give it the .ipynb extension. This will automatically enable Jupyter notebook functionality. Now you can write code and display results directly in your IDE without launching a web browser. Pretty slick!

I’ve found Code’s Jupyter notebooks to be very reliable. In fact, I don’t think I’ve encountered any problems at all.

That said, notebooks are not always the best format to write Python code, so it’s a good idea to familiarize yourself with some of the methods listed above so you’re ready when you need them.

A Jupyter notebook that is being rendered and edited in Visual Studio Code.


At first, Visual Studio Code may seem a little daunting when you’re trying to run Python code. But after you’ve run a few scripts you’ll find a method that works best for your applications and that is comfortable for you. Visual Studio Code is a powerful, free code editor. Before you give up on it for running your Python code give these methods a try. They’ve worked for me, they should work for you.

Similar Posts