Python in Visual Studio Code
Working with Python in Visual Studio Code, using the Microsoft Python extension, is simple, fun, and productive. The extension makes VS Code an excellent Python editor, and works on any operating system with a variety of Python interpreters. It leverages all of VS Code’s power to provide auto complete and IntelliSense, linting, debugging, and unit testing, along with the ability to easily switch between Python environments, including virtual and conda environments.
This article provides only an overview of the different capabilities of the Python extension for VS Code. For a walkthrough of editing, running, and debugging code, use the button below.
Install Python and the Python extension
The tutorial guides you through installing Python and using the extension. You must install a Python interpreter yourself separately from the extension. For a quick install, use Python from python.org and install the extension from the VS Code Marketplace.
Note: To help get you started with Python development, you can use the Python profile template that includes useful extensions, settings, and Python code snippets.
Once you have a version of Python installed, select it using the Python: Select Interpreter command. If VS Code doesn’t automatically locate the interpreter you’re looking for, refer to Environments — Manually specify an interpreter.
You can configure the Python extension through settings. Learn more in the Python Settings reference.
Windows Subsystem for Linux: If you are on Windows, WSL is a great way to do Python development. You can run Linux distributions on Windows and Python is often already installed. When coupled with the WSL extension, you get full VS Code editing and debugging support while running in the context of WSL. To learn more, go to Developing in WSL or try the Working in WSL tutorial.
Run Python code
To experience Python, create a file (using the File Explorer) named hello.py and paste in the following code:
print("Hello World")
The Python extension then provides shortcuts to run Python code using the currently selected interpreter (Python: Select Interpreter in the Command Palette). To run the active Python file, click the Run Python File in Terminal play button in the top-right side of the editor.
You can also run individual lines or a selection of code with the Python: Run Selection/Line in Python Terminal command ( Shift+Enter ). If there isn’t a selection, the line with your cursor will be run in the Python Terminal. An identical Run Selection/Line in Python Terminal command is available on the context menu for a selection in the editor. The same terminal will be used every time you run a selection or a line in the terminal/REPL, until that terminal is closed. The same terminal is also used for Run Python File in Terminal. If that terminal is still running the REPL, you should exit the REPL ( exit() ) or switch to a different terminal before running a Python file.
The Python extension automatically removes indents based on the first non-empty line of the selection, shifting all other lines left as needed.
The command opens the Python Terminal if necessary; you can also open the interactive REPL environment directly using the Python: Start REPL command that activates a terminal with the currently selected interpreter and then runs the Python REPL.
For a more specific walkthrough and other ways of running code, see the run code tutorial.
Autocomplete and IntelliSense
The Python extension supports code completion and IntelliSense using the currently selected interpreter. IntelliSense is a general term for a number of features, including intelligent code completion (in-context method and variable suggestions) across all your files and for built-in and third-party modules.
IntelliSense quickly shows methods, class members, and documentation as you type. You can also trigger completions at any time with ⌃Space (Windows, Linux Ctrl+Space ) . Hovering over identifiers will show more information about them.
Enhance completions with AI
GitHub Copilot is an AI-powered code completion tool that helps you write code faster and smarter. You can use the GitHub Copilot extension in VS Code to generate code, or to learn from the code it generates.

GitHub Copilot provides suggestions for languages beyond Python and a wide variety of frameworks, including JavaScript, TypeScript, Ruby, Go, C# and C++.
You can learn more about how to get started with Copilot in the Copilot documentation.
Linting
Linting analyzes your Python code for potential errors, making it easy to navigate to and correct different problems.
The Python extension can apply a number of different linters including Pylint, pycodestyle, Flake8, mypy, pydocstyle, prospector, and pylama. See Linting.
Debugging
No more print statement debugging! VS Code comes with great debugging support for Python, allowing you to set breakpoints, inspect variables, and use the debug console for an in-depth look at how your program is executing step by step. Debug a number of different types of Python applications, including multi-threaded, web, and remote applications.
For more specific information on debugging in Python, such as configuring your launch.json settings and implementing remote debugging, see Debugging. General VS Code debugging information is found in the debugging document.
Additionally, the Django and Flask tutorials provide examples of how to implement debugging in the context of web applications, including debugging Django templates.
Environments
The Python extension automatically detects Python interpreters that are installed in standard locations. It also detects conda environments as well as virtual environments in the workspace folder. See Configuring Python environments.
The current environment is shown on the right side of the VS Code Status Bar:

The Status Bar also indicates if no interpreter is selected:

The selected environment is used for IntelliSense, auto-completions, linting, formatting, and any other language-related feature. It is also activated when you run or debug Python in a terminal, or when you create a new terminal with the Terminal: Create New Terminal command.
To change the current interpreter, which includes switching to conda or virtual environments, select the interpreter name on the Status Bar or use the Python: Select Interpreter command.

VS Code prompts you with a list of detected environments as well as any you’ve added manually to your user settings (see Configuring Python environments).
Jupyter notebooks
To enable Python support for Jupyter notebook files ( .ipynb ) in VS Code, you can install the Jupyter extension. The Python and Jupyter extensions work together to give you a great Notebook experience in VS Code, providing you the ability to directly view and modify code cells with IntelliSense support, as well as run and debug them.

You can also convert and open the notebook as a Python code file through the Jupyter: Export to Python Script command. The notebook’s cells are delimited in the Python file with #%% comments, and the Jupyter extension shows Run Cell or Run Below CodeLens. Selecting either CodeLens starts the Jupyter server and runs the cell(s) in the Python interactive window:

You can also connect to a remote Jupyter server to run your notebooks. For more information, see Jupyter support.
Testing
The Python extension supports testing with Python’s built-in unittest framework and pytest.
In order to run tests, you must enable one of the supported testing frameworks in the settings of your project. Each framework has its own specific settings, such as arguments for identifying the paths and patterns for test discovery.
Once the tests have been discovered, VS Code provides a variety of commands (on the Status Bar, the Command Palette, and elsewhere) to run and debug tests. These commands also allow you to run individual test files and methods
Configuration
The Python extension provides a wide variety of settings for its various features. These are described on their relevant topics, such as Editing code, Linting, Debugging, and Testing. The complete list is found in the Settings reference.
Python profile template
Profiles let you quickly switch your extensions, settings, and UI layout depending on your current project or task. To help you get started with Python development, you can use the Python profile template, which is a curated profile with useful extensions, settings, and snippets. You can use the profile template as is or use it as a starting point to customize further for you own workflows.
You select a profile template through the Profiles > Create Profile. dropdown:

Once you select a profile template, you can review the settings and extensions, and remove individual items if you don’t want to include them in your new Profile. After creating the new profile based on the template, changes made to settings, extensions, or UI are persisted in your profile.
Other popular Python extensions
The Microsoft Python extension provides all of the features described previously in this article. Additional Python language support can be added to VS Code by installing other popular Python extensions.
- Open the Extensions view ( ⇧⌘X (Windows, Linux Ctrl+Shift+X ) ).
- Filter the extension list by typing ‘python’.
The extensions shown above are dynamically queried. Click on an extension tile above to read the description and reviews to decide which extension is best for you. See more in the Marketplace.
Next steps
- Python Hello World tutorial — Get started with Python in VS Code.
- Editing Python — Learn about auto-completion, formatting, and refactoring for Python.
- Basic Editing — Learn about the powerful VS Code editor.
- Code Navigation — Move quickly through your source code.
- Django tutorial
- Flask tutorial
Python in Visual Studio Code
Working with Python in Visual Studio Code, using the Microsoft Python extension, is simple, fun, and productive. The extension makes VS Code an excellent Python editor, and works on any operating system with a variety of Python interpreters. It leverages all of VS Code’s power to provide auto complete and IntelliSense, linting, debugging, and unit testing, along with the ability to easily switch between Python environments, including virtual and conda environments.
This article provides only an overview of the different capabilities of the Python extension for VS Code. For a walkthrough of editing, running, and debugging code, use the button below.
Install Python and the Python extension
The tutorial guides you through installing Python and using the extension. You must install a Python interpreter yourself separately from the extension. For a quick install, use Python from python.org and install the extension from the VS Code Marketplace.
Note: To help get you started with Python development, you can use the Python profile template that includes useful extensions, settings, and Python code snippets.
Once you have a version of Python installed, select it using the Python: Select Interpreter command. If VS Code doesn’t automatically locate the interpreter you’re looking for, refer to Environments — Manually specify an interpreter.
You can configure the Python extension through settings. Learn more in the Python Settings reference.
Windows Subsystem for Linux: If you are on Windows, WSL is a great way to do Python development. You can run Linux distributions on Windows and Python is often already installed. When coupled with the WSL extension, you get full VS Code editing and debugging support while running in the context of WSL. To learn more, go to Developing in WSL or try the Working in WSL tutorial.
Run Python code
To experience Python, create a file (using the File Explorer) named hello.py and paste in the following code:
print("Hello World")
The Python extension then provides shortcuts to run Python code using the currently selected interpreter (Python: Select Interpreter in the Command Palette). To run the active Python file, click the Run Python File in Terminal play button in the top-right side of the editor.

You can also run individual lines or a selection of code with the Python: Run Selection/Line in Python Terminal command ( Shift+Enter ). If there isn’t a selection, the line with your cursor will be run in the Python Terminal. An identical Run Selection/Line in Python Terminal command is available on the context menu for a selection in the editor. The same terminal will be used every time you run a selection or a line in the terminal/REPL, until that terminal is closed. The same terminal is also used for Run Python File in Terminal. If that terminal is still running the REPL, you should exit the REPL ( exit() ) or switch to a different terminal before running a Python file.
The Python extension automatically removes indents based on the first non-empty line of the selection, shifting all other lines left as needed.
The command opens the Python Terminal if necessary; you can also open the interactive REPL environment directly using the Python: Start REPL command that activates a terminal with the currently selected interpreter and then runs the Python REPL.
For a more specific walkthrough and other ways of running code, see the run code tutorial.
Autocomplete and IntelliSense
The Python extension supports code completion and IntelliSense using the currently selected interpreter. IntelliSense is a general term for a number of features, including intelligent code completion (in-context method and variable suggestions) across all your files and for built-in and third-party modules.
IntelliSense quickly shows methods, class members, and documentation as you type. You can also trigger completions at any time with ⌃Space (Windows, Linux Ctrl+Space ) . Hovering over identifiers will show more information about them.
Enhance completions with AI
GitHub Copilot is an AI-powered code completion tool that helps you write code faster and smarter. You can use the GitHub Copilot extension in VS Code to generate code, or to learn from the code it generates.

GitHub Copilot provides suggestions for languages beyond Python and a wide variety of frameworks, including JavaScript, TypeScript, Ruby, Go, C# and C++.
You can learn more about how to get started with Copilot in the Copilot documentation.
Linting
Linting analyzes your Python code for potential errors, making it easy to navigate to and correct different problems.
The Python extension can apply a number of different linters including Pylint, pycodestyle, Flake8, mypy, pydocstyle, prospector, and pylama. See Linting.
Debugging
No more print statement debugging! VS Code comes with great debugging support for Python, allowing you to set breakpoints, inspect variables, and use the debug console for an in-depth look at how your program is executing step by step. Debug a number of different types of Python applications, including multi-threaded, web, and remote applications.
For more specific information on debugging in Python, such as configuring your launch.json settings and implementing remote debugging, see Debugging. General VS Code debugging information is found in the debugging document.
Additionally, the Django and Flask tutorials provide examples of how to implement debugging in the context of web applications, including debugging Django templates.
Environments
The Python extension automatically detects Python interpreters that are installed in standard locations. It also detects conda environments as well as virtual environments in the workspace folder. See Configuring Python environments.
The current environment is shown on the right side of the VS Code Status Bar:

The Status Bar also indicates if no interpreter is selected:

The selected environment is used for IntelliSense, auto-completions, linting, formatting, and any other language-related feature. It is also activated when you run or debug Python in a terminal, or when you create a new terminal with the Terminal: Create New Terminal command.
To change the current interpreter, which includes switching to conda or virtual environments, select the interpreter name on the Status Bar or use the Python: Select Interpreter command.

VS Code prompts you with a list of detected environments as well as any you’ve added manually to your user settings (see Configuring Python environments).
Jupyter notebooks
To enable Python support for Jupyter notebook files ( .ipynb ) in VS Code, you can install the Jupyter extension. The Python and Jupyter extensions work together to give you a great Notebook experience in VS Code, providing you the ability to directly view and modify code cells with IntelliSense support, as well as run and debug them.

You can also convert and open the notebook as a Python code file through the Jupyter: Export to Python Script command. The notebook’s cells are delimited in the Python file with #%% comments, and the Jupyter extension shows Run Cell or Run Below CodeLens. Selecting either CodeLens starts the Jupyter server and runs the cell(s) in the Python interactive window:

You can also connect to a remote Jupyter server to run your notebooks. For more information, see Jupyter support.
Testing
The Python extension supports testing with Python’s built-in unittest framework and pytest.
In order to run tests, you must enable one of the supported testing frameworks in the settings of your project. Each framework has its own specific settings, such as arguments for identifying the paths and patterns for test discovery.
Once the tests have been discovered, VS Code provides a variety of commands (on the Status Bar, the Command Palette, and elsewhere) to run and debug tests. These commands also allow you to run individual test files and methods
Configuration
The Python extension provides a wide variety of settings for its various features. These are described on their relevant topics, such as Editing code, Linting, Debugging, and Testing. The complete list is found in the Settings reference.
Python profile template
Profiles let you quickly switch your extensions, settings, and UI layout depending on your current project or task. To help you get started with Python development, you can use the Python profile template, which is a curated profile with useful extensions, settings, and snippets. You can use the profile template as is or use it as a starting point to customize further for you own workflows.
You select a profile template through the Profiles > Create Profile. dropdown:

Once you select a profile template, you can review the settings and extensions, and remove individual items if you don’t want to include them in your new Profile. After creating the new profile based on the template, changes made to settings, extensions, or UI are persisted in your profile.
Other popular Python extensions
The Microsoft Python extension provides all of the features described previously in this article. Additional Python language support can be added to VS Code by installing other popular Python extensions.
- Open the Extensions view ( ⇧⌘X (Windows, Linux Ctrl+Shift+X ) ).
- Filter the extension list by typing ‘python’.
The extensions shown above are dynamically queried. Click on an extension tile above to read the description and reviews to decide which extension is best for you. See more in the Marketplace.
Next steps
- Python Hello World tutorial — Get started with Python in VS Code.
- Editing Python — Learn about auto-completion, formatting, and refactoring for Python.
- Basic Editing — Learn about the powerful VS Code editor.
- Code Navigation — Move quickly through your source code.
- Django tutorial
- Flask tutorial
Python Development in Visual Studio Code
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Python Development in Visual Studio Code (Setup Guide)
One of the coolest code editors available to programmers, Visual Studio Code, is an open-source, extensible, light-weight editor available on all platforms. It’s these qualities that make Visual Studio Code from Microsoft very popular, and a great platform for Python development.
In this article, you’ll learn about Python development in Visual Studio Code, including how to:
- Install Visual Studio Code
- Discover and install extensions that make Python development easy
- Write a straightforward Python application
- Learn how to run and debug existing Python programs in VS Code
- Connect Visual Studio Code to Git and GitHub to share your code with the world
We assume you are familiar with Python development and already have some form of Python installed on your system (Python 2.7, Python 3.6/3.7, Anaconda, or others). Screenshots and demos for Ubuntu and Windows are provided. Because Visual Studio Code runs on all major platforms, you may see slightly different UI elements and may need to modify certain commands.
If you already have a basic VS Code setup and you’re hoping to dig deeper than the goals in this tutorial, you might want to explore some advanced features in VS Code.
Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.
Installing and Configuring Visual Studio Code for Python Development
Installing Visual Studio Code is very accessible on any platform. Full instructions for Windows, Mac, and Linux are available, and the editor is updated monthly with new features and bug fixes. You can find everything at the Visual Studio Code website:

In case you were wondering, Visual Studio Code (or VS Code for short) shares almost nothing other than a name with its larger Windows-based namesake, Visual Studio.
Note: To learn how to set up VS Code as part of a full Python coding environment on a Windows machine, check out this comprehensive guide.
Visual Studio Code has built-in support for multiple languages and an extension model with a rich ecosystem of support for others. VS Code is updated monthly, and you can keep up to date at the Microsoft Python blog. Microsoft even makes the VS Code GitHub repo available for anyone to clone and contribute. (Cue the PR flood.)
The VS Code UI is well documented, so I won’t rehash it here:

Extensions for Python Development
As stated above, VS Code supports development in multiple programming languages through a well-documented extension model. The Python extension enables Python development in Visual Studio Code, with the following features:

- Support for Python 3.4 and higher, as well as Python 2.7
- Code completion with IntelliSense
- Linting
- Debugging support
- Code snippets
- Unit testing support
- Automatic use of conda and virtual environments
- Code editing in Jupyter environments and Jupyter Notebooks
Visual Studio Code extensions cover more than just programming language capabilities:
- Keymaps allow users already familiar with Atom, Sublime Text, Emacs, Vim, PyCharm, or other environments to feel at home.
- Themes customize the UI whether you like coding in the light, dark, or something more colorful.
- Language packs provide a localized experience.
Here are some other extensions and settings I find useful:
- GitLens provides tons of useful Git features directly in your editing window, including blame annotations and repository exploration features.
- Auto save is easily turned on by selecting File, Auto Save from the menu. The default delay time is 1000 milliseconds, which is also configurable.
- Settings Sync allows you to synchronize your VS Code settings across different installations using GitHub. If you work on different machines, this helps keep your environment consistent across them.
- Docker lets you quickly and easily work with Docker, helping author Dockerfile and docker-compose.yml , package and deploy your projects, and even generate the proper Docker files for your project.
Of course, you may discover other useful extensions as you use VS Code. Please share your discoveries and settings in the comments!
Discovering and installing new extensions and themes is accessible by clicking on the Extensions icon on the Activity Bar. You can search for extensions using keywords, sort the results numerous ways, and install extensions quickly and easily. For this article, install the Python extension by typing python in the Extensions item on the Activity Bar, and clicking Install:
You can find and install any of the extensions mentioned above in the same manner.
Visual Studio Code Configuration Files
One important thing to mention is that Visual Studio Code is highly configurable through user and workspace settings.
User settings are global across all Visual Studio Code instances, while workspace settings are local to the specific folder or project workspace. Workspace settings give VS Code tons of flexibility, and I call out workspace settings throughout this article. Workspace settings are stored as .json files in a folder local to the project workspace called .vscode .
Start a New Python Program
Let’s start our exploration of Python development in Visual Studio Code with a new Python program. In VS Code, type Ctrl + N to open a new File. (You can also select File, New from the menu.)
Note: The Visual Studio Code UI provides the Command Palette, from which you can search and execute any command without leaving the keyboard. Open the Command Palette using Ctrl + Shift + P , type File: New File , and hit Enter to open a new file.
No matter how you get there, you should see a VS Code window that looks similar to the following:

Once a new file is opened, you can begin entering code.
Entering Python Code
For our test code, let’s quickly code up the Sieve of Eratosthenes (which finds all primes less than a given number). Begin typing the following code in the new tab you just opened:
sieve = [True] * 101 for i in range(2, 100):
You should see something similar to this:

Wait, what’s going on? Why isn’t Visual Studio Code doing any keyword highlighting, any auto-formatting, or anything really helpful? What gives?
The answer is that, right now, VS Code doesn’t know what kind of file it’s dealing with. The buffer is called Untitled-1 , and if you look in the lower right corner of the window, you’ll see the words Plain Text.
To activate the Python extension, save the file (by selecting File, Save from the menu, File:Save File from the Command Palette, or just using Ctrl + S ) as sieve.py . VS Code will see the .py extension and correctly interpret the file as Python code. Now your window should look like this:

That’s much better! VS Code automatically reformats the file as Python, which you can verify by inspecting the language mode in the lower left corner.
If you have multiple Python installations (like Python 2.7, Python 3.x, or Anaconda), you can change which Python interpreter VS Code uses by clicking the language mode indicator, or selecting Python: Select Interpreter from the Command Palette. VS Code supports formatting using pep8 by default, but you can select black or yapf if you wish.
Here’s the full code for a basic Sieve of Eratosthenes:
sieve = [True] * 101 for i in range(2, 100): if sieve[i]: print(i) for j in range(i*i, 100, i): sieve[j] = False
As you type this code, VS Code automatically indents the lines under for and if statements for you properly, adds closing parentheses, and makes suggestions for you. That’s the power of IntelliSense working for you.
Running Python Code
Now that the code is complete, you can run it. There is no need to leave the editor to do this: Visual Studio Code can run this program directly in the editor. Save the file (using Ctrl + S ), then right-click in the editor window and select Run Python File in Terminal:

When Visual Studio Code opens the folder, it also opens the files you last had opened. (This is configurable.) You can open, edit, run, and debug any file listed. The Explorer view in the Activity Bar on the left gives you a view of all the files in the folder and shows how many unsaved files exist in the current set of tabs.
Testing Support
VS Code can automatically recognize existing Python tests written in the unittest framework, or the pytest or Nose frameworks if those frameworks are installed in the current environment. I have a unit test written in unittest for the equation eval library, which you can use for this example.
To run your existing unit tests, from any Python file in the project, right-click and select Run Current Unit Test File. You’ll be prompted to specify the test framework, where in the project to search for tests, and the filename pattern your tests utilize.
All of these are saved as workspace settings in your local .vscode/settings.json file and can be modified there. For this equation project, you select unittest , the current folder, and the pattern *_test.py .
Once the test framework is set up and the tests have been discovered, you can run all your tests by clicking Run Tests on the Status Bar and selecting an option from the Command Palette:

The debugger can control Python apps running in the built-in terminal or an external terminal instance. It can attach to an already running Python instances, and can even debug Django and Flask apps.
Debugging code in a single Python file is as simple as starting the debugger using F5 . You use F10 and F11 to step over and into functions respectively, and Shift + F5 to exit the debugger. Breakpoints are set using F9 , or using the mouse by clicking in the left margin in the editor window.
Before you start debugging more complicated projects, including Django or Flask applications, you need to setup and then select a debug configuration. Setting up the debug configuration is relatively straightforward. From the Debug view, select the Configuration drop-down, then Add Configuration, and select Python:

Visual Studio Code will create a debug configuration file under the current folder called .vscode/launch.json , which allows you to setup specific Python configurations as well as settings for debugging specific apps, like Django and Flask.
You can even perform remote debugging, and debug Jinja and Django templates. Close the launch.json file in the editor and select the proper configuration for your application from the Configuration drop-down.
Git Integration
VS Code has built-in support for source control management, and ships with support for Git and GitHub right out of the box. You can install support for other SCM’s in VS Code, and use them side by side. Source control is accessible from the Source Control view:

If your project folder contains a .git folder, VS Code automatically turns on the full range of Git/GitHub functionality. Here are some of the many tasks you can perform:
- Commit files to Git
- Push changes to, and pull changes from, remote repos
- Check-out existing or create new branches and tags
- View and resolve merge conflicts
- View diffs
All of this functionality is available directly from the VS Code UI:

VS Code will also recognize changes made outside the editor and behave appropriately.
Committing your recent changes within VS Code is a fairly straightforward process. Modified files are shown in the Source Control view with an M marker, while new untracked files are marked with a U. Stage your changes by hovering over the file and then clicking the plus sign (+). Add a commit message at the top of the view, and then click the check mark to commit the changes:

You can push local commits to GitHub from within VS Code as well. Select Sync from the Source Control view menu, or click Synchronize Changes on the status bar next to the branch indicator.
Conclusion
Visual Studio Code is one of the coolest general purpose editors and a great candidate for Python development. In this article, you learned:
- How to install VS Code on any platform
- How to find and install extensions to enable Python-specific features
- How VS Code makes writing a simple Python application easier
- How to run and debug existing Python programs within VS Code
- How to work with Git and GitHub repositories from VS Code
Visual Studio Code has become my default editor for Python and other tasks, and I hope you give it a chance to become yours as well.
If you have questions or comments, please reach out in the comments below. There is also a lot more information at the Visual Studio Code website than we could cover here.
The author sends thanks to Dan Taylor from the Visual Studio Code team at Microsoft for his time and invaluable input in this article.
Mark as Completed
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Python Development in Visual Studio Code (Setup Guide)
Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

About Jon Fincher
Jon taught Python and Java in two high schools in Washington State. Previously, he was a Program Manager at Microsoft.
Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:


Master Real-World Python Skills With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
Master Real-World Python Skills
With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
What Do You Think?
Rate this article:
What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.
Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal. Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!
How To Get Started With Python in Visual Studio Code

Python is one of the most popular and easy to learn languages, which is why it is often one of the first languages you learn. Let’s see how to work with and run Python inside of Visual Studio Code.
In this tutorial you’ll install the Python extension then use intellisense and shortcuts to run your Python code.
Prerequisites
- Python installed on your machine and a local development environment set up. You can complete both of these with our tutorial How To Install and Set Up a Local Programming Environment for Python 3.
- Visual Studio Code installed on your machine by visiting the official download page.
Step 1 — Running Python From the Built-in Terminal
With Python installed and your local programming environment set up, open Visual Studio Code.
Inside of Visual Studio Code, open the directory you’re working in by going to File -> Open and selecting the directory. After that, you’ll see your folder open in the explorer window on the left.

With the directory open, you can create your first Python file ( .py extension) with some code to print «Hello World» .

Now that you have your Hello World code ready, we can run it by using the built-in terminal in Visual Studio Code. If if is not open already, you can open it by going to View -> Terminal or use the shortcut, CTRL+~ .

The terminal that you just opened will automatically start in the current directory that you are editing in Visual Studio Code. This is exactly why we created and opened a directory before getting started. We can prove this by running the following command:
This command will print the path to the current directory. From there, you can verify that your Python file is also inside of the current directory by running the following command to print a list of files in the directory:
Now, you can run your Python file with the following command:
After running, you’ll see Hello World printed out in the console.

Step 2 — Installing the Python Extension
We can streamline the process of working with Python in Visual Studio by installing the Python extension created by Microsoft. To install the extension, open up the extension menu on the left (the icon looks like a square inside of a square) and search Python.
It will be the first one that pops up, and you can click on it to view the extension details and click Install.

After installing, you might need to reload, so go ahead and do that.
After you restart, you can now take advantage of the Python extension’s features:
- IntelliSense
- Auto-completion
- Shortcuts for running Python Files
- Additional info on hovering Python variables, functions, and so on.
To start working with IntelliSense, create an empty array called list .
list = []
Then following type list. followed by a period and notice that some information pops up. The extension is providing you all the functions and properties of a list that you can use.

If you want to use one of those functions, you can press ENTER or TAB to auto-complete that function name. This means that don’t have to memorize every function in Python because the extension will give you hints as to what is available. Notice also that it shows you a brief description of what the function does and what parameters it takes.
You can also get intellisense when importing modules in Python. Notice if you type random , intellisense pops up to complete the name of the module as well as providing some background info on what it does.

If you then start to use the random module, you’ll continue to get intellisense for functions that you can access with that module.

Lastly, you can hover on existing variables, module imports, and so on, for additional information whenever you need it.

Step 3 — Using Shortcuts to Run Python Code
If you want to do more in your Python file, here’s a short snippet for the Bubble Sort algorithm. It calls the bubble_sort function and prints out the result. You can copy this code into your file:
def bubble_sort(list): sorted_list = list[:] is_sorted = False while is_sorted == False: swaps = 0 for i in range(len(list) - 1): if sorted_list[i] > sorted_list[i + 1]: # swap temp = sorted_list[i] sorted_list[i] = sorted_list[i + 1] sorted_list[i + 1] = temp swaps += 1 print(swaps) if swaps == 0: is_sorted = True return sorted_list print(bubble_sort([2, 1, 3]))
With this new piece of code, let’s explore a new way to run our Python file. The typical first workflow for working with Python files is to save your file and then run that Python file in the terminal. With the Python extension, there are a few shortcuts to help with this process.
Inside of any Python file, you can right click in the editor and choose Run Python File In Terminal. This command will do each of the individual steps that we talked about before.

After using the shortcut, you can see the bubble_sort output in your console.

You also have a shortcut to open the Python REPL where you can quickly type Python code directly into your console and see the output. Open the command palette using the shortcut CMD+SHIFT+P on Mac or CTRL+SHIFT+P on Windows and select Python Start REPL.

After typing in a print command, you will see Hello World immediately displayed in the console.

Conclusion
Python is an incredibly popular language with strong support in Visual Studio Code. By installing the Python extension, you’ll get Python intellisense, auto-completion, and other useful miscellaneous shortcuts.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
