Basics Intermediate Advanced
aialgorithmsapibest-practicescareercommunitydatabasesdata-sciencedata-structuresdata-vizdevopsdjangodockereditorsflaskfront-endgamedevguimachine-learningnewsnumpyprojectspythonstdlibtestingtoolsweb-devweb-scraping
Recommended Course

Python Development in Visual Studio Code (Setup Guide)
29m · 6 lessons

Python Development in Visual Studio Code
Table of Contents
Recommended Course
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 fromMicrosoft 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 installextensions that make Python development easy
- Write a straightforwardPython application
- Learn how torun anddebug existing Python programs in VS Code
- Connect Visual Studio Code to Git and GitHub toshare 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 someadvanced 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 isvery accessible on any platform. Full instructions forWindows,Mac, andLinux are available, and the editor is updated monthly with new features and bug fixes. You can find everything at theVisual 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 thiscomprehensive 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 theMicrosoft Python blog. Microsoft even makes theVS Code GitHub repo available for anyone to clone and contribute. (Cue the PR flood.)
TheVS 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-documentedextension model. ThePython 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 withIntelliSense
- Linting
- Debugging support
- Code snippets
- Unit testing support
- Automatic use ofconda and virtual environments
- Code editing inJupyter environments andJupyter 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 Savefrom the menu. The default delay time is 1000 milliseconds, which is alsoconfigurable.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
Dockerfileanddocker-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 thecomments!
Discovering and installing new extensions and themes is accessible by clicking on theExtensions 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 typingpython in theExtensions item on the Activity Bar, and clickingInstall:

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 throughuser 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, typeCtrl+N to open a new File. (You can also selectFile, New from the menu.)
Note: The Visual Studio Code UI provides theCommand Palette, from which you can search and execute any command without leaving the keyboard. Open the Command Palette usingCtrl+Shift+P, typeFile: New File, and hitEnter 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 theSieve 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]*101foriinrange(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 calledUntitled-1, and if you look in the lower right corner of the window, you’ll see the wordsPlain Text.
To activate the Python extension, save the file (by selectingFile, Save from the menu,File:Save File from the Command Palette, or just usingCtrl+S) assieve.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 selectingPython: Select Interpreter from the Command Palette. VS Code supportsformatting usingpep8 by default, but you can selectblack oryapf if you wish.
Let’s add the rest of the Sieve code now. To see IntelliSense at work, type this code directly rather than cut and paste, and you should see something like this:

Here’s the full code for a basic Sieve of Eratosthenes:
sieve=[True]*101foriinrange(2,100):ifsieve[i]:print(i)forjinrange(i*i,100,i):sieve[j]=FalseAs you type this code, VS Code automatically indents the lines underfor andif 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 (usingCtrl+S), then right-click in the editor window and selectRun Python File in Terminal:
You should see the Terminal pane appear at the bottom of the window, with your code output showing.
Python Linting Support
You may have seen a pop up appear while you were typing, stating that linting was not available. You can quickly install linting support from that pop up, which defaults toPyLint. VS Code also supports other linters. Here’s the complete list at the time of this writing:
pylintflake8mypypydocstylepep8prospectorpyllamabandit
ThePython linting page has complete details on how to setup each linter.
Note: The choice of linter is a project workspace setting, and not a global user setting.
Editing an Existing Python Project
In the Sieve of Eratosthenes example, you created a single Python file. That’s great as an example, but many times, you’ll create larger projects and work on them over a longer period of time. A typical new project work flow might look like this:
- Create a folder to hold the project (which may include a new GitHub project)
- Change to the new folder
- Create the initial Python code using the command
code filename.py
Using Visual Studio Code on a Python project (as opposed to a single Python file) opens up tons more functionality that lets VS Code truly shine. Let’s take a look at how it works with a larger project.
Late in the previous millennium, when I was a much younger programmer, I wrote a calculator program that parsed equations written in infix notation, using an adaptation of Edsger Dijkstra’sshunting yard algorithm.
To demonstrate the project-focused features of Visual Studio Code, I began recreating the shunting yard algorithm as anequation evaluation library in Python. To continue following along, feel free to clone the repo locally.
Once the folder is created locally, you can open the entire folder in VS Code quickly. My preferred method (as mentioned above) is modified as follows, since I already have the folder and basic files created:
cd/path/to/projectcode.VS Code understands, and will use, anyvirtualenv,pipenv, orconda environments it sees when opened this way. You don’t even need to start the virtual environment first! You can even open a folder from the UI, usingFile, Open Folder from the menu,Ctrl+K,Ctrl+O from the keyboard, orFile:Open Folder from the Command Palette.
For my equation eval library project, here’s what I see:

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 recognizeexisting Python tests written in theunittest framework, or thepytest orNose frameworks if those frameworks are installed in the current environment. I have aunit test written inunittest 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 selectRun 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 selectunittest, 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 clickingRun Tests on the Status Bar and selecting an option from the Command Palette:
You can even run individual tests by opening the test file in VS Code, clickingRun Tests on the Status Bar, and selecting theRun Unit Test Method… and the specific test to run. This makes it trivial to address individual test failures and re-run only failed tests, which is a huge time-saver! Test results are shown in theOutput pane underPython Test Log.
Debugging Support
Even though VS Code is a code editor,debugging Python directly within VS Code is possible. VS Code offers many of the features you would expect from a good code debugger, including:
- Automaticvariable tracking
- Watch expressions
- Breakpoints
- Call stack inspection
You can see them all as part of theDebug view on the Activity Bar:

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 debugDjango andFlask apps.
Debugging code in a single Python file is as simple as starting the debugger usingF5. You useF10 andF11 to step over and into functions respectively, andShift+F5 to exit the debugger. Breakpoints are set usingF9, or using the mouse by clicking in the left margin in the editor window.
Before you start debugging more complicated projects, includingDjango or Flask applications, you need to setup and then select a debug configuration. Setting up the debug configuration is relatively straightforward. From theDebug view, select theConfiguration drop-down, thenAdd Configuration, and selectPython:

Visual Studio Code will create a debug configuration file under the current folder called.vscode/launch.json, which allows you to setup specificPython configurations as well as settings fordebugging specific apps, like Django and Flask.
You can even perform remote debugging, and debug Jinja and Django templates. Close thelaunch.json file in the editor and select the proper configuration for your application from theConfiguration drop-down.
Git Integration
VS Code has built-in support forsource 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 theSource Control view:

If your project folder contains a.git folder, VS Code automatically turns on the full range ofGit/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 newbranches and tags
- View and resolvemerge 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 theSource Control view with anM marker, while new untracked files are marked with aU. 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. SelectSync from theSource Control view menu, or clickSynchronize Changes on thestatus 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 theVisual Studio Code website than we could cover here.
The author sends thanks toDan Taylor from the Visual Studio Code team at Microsoft for his time and invaluable input in this article.
Recommended Course
🐍 Python Tricks 💌
Get a short & sweetPython Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

AboutJon Fincher
Jon taught Python and Java in two high schools in Washington State. Previously, he was a Program Manager at Microsoft.
» More about JonMasterReal-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:
MasterReal-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?
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 andget answers to common questions in our support portal.
Looking for a real-time conversation? Visit theReal Python Community Chat or join the next“Office Hours” Live Q&A Session. Happy Pythoning!
Keep Learning
Keep reading Real Python by creating a free account or signing in:
Already have an account?Sign-In




