In this tutorial, you will learn how to set up Visual Studio Code for Python development. We go through the process from start to finish. Make sure you have Python installed before following this tutorial.
Table of Contents
- Can I use Visual Studio Code for Python?
- Is Visual Studio Code Good For Beginners?
- Step 1 – Install VSCode for Python Development
- Step 2 – Configure VSCode for Python Development
- Additional Extensions
- Summary
Can I use Visual Studio Code for Python?
Visual Studio Code provides full support for Python development and has a wide array of official Python plugins available that make the Python development experience better.
In our opinion, Visual Studio Code is the best code editor for Python development due to its easy-to-use interface and customizability while not being overwhelming for someone who is just starting out.
Is Visual Studio Code Good For Beginners?
Yes. Visual Studio Code is a great choice for beginners since it’s completely free to use. Visual Studio Code is the most popular code editor for Python today and is used by millions of people. Since it is developed by Microsoft, it runs flawlessly on Windows operating systems but also has versions available for Linux and macOS.
Step 1 – Install VSCode for Python Development
First off, we need to install VSCode. You can download VSCode here. In this example, we are using Windows 11 to install VSCode, so we are choosing the Windows installer. If you use Linux or macOS, choose accordingly.
After the download has finished, launch the installer and install VSCode, leaving everything on default. Make sure the Add to PATH (requires shell restart) option is enabled:
Step 2 – Configure VSCode for Python Development
After installing VSCode, open it up. Once open, either quickly walk through the starting screen or just skip it.
Installing Python Extensions
Now we need to configure VSCode to work with Python. Therefore, we need to install the VSCode Python extension. Click on the little cog wheel in the lower-left corner and select Extensions.
Once there, search for Python. Install the official Python extension from Microsoft. Note that Jupyter Notebook, another official Microsoft extension for Python, will also get installed automatically.
This adds Python support, Python IntelliSense, Linting, and more.
Choosing a Formatter
Next, we set up a Default Formatter for our Python files. This formatter will format your code every time you run the Format Document command.
Alternatively, we can set up the Format on Save feature, which will automatically format your document every time you save your Python file (recommended.)
Open VSCode, click on File, and select New File. Select Python File from the dropdown menu. Press CTRL + S to save your file and name it test.py
.
Once that is done, press SHIFT + ALT + F to run the format document command. This will trigger an event that tells you that you don’t have a default formatter installed and recommends you to install autopep8 or Black.
The choice is yours, but we are going to install autopep8. Once autopep8 is installed, you are now able to format your document automatically by pressing SHIFT + ALT + F.
Format on Save
To make things more convenient, let’s set up auto-formatting next. Press SHIFT + CTRL + P on your keyboard and type User Settings (JSON) into the search bar. Open the User Settings JSON file. If you do not have any other extension installed than the ones we just installed, your settings.json
file should look like this:
{}
Code language: JSON / JSON with Comments (json)
Or maybe it is completely empty.
To enable Format on Save for Python in VSCode, change your file to look like this:
{
"editor.formatOnSave": true,
}
Code language: JSON / JSON with Comments (json)
Save the file and close it. Now, every time you save your code, autopep8 (or Black) should automatically format it.
Additional Extensions
There are some additional, optional extensions that we found to be valuable for a Python developer. If you choose so, feel free to play around with them. As of now, we only recommend the following. More will be added if we see fit:
- Python Indent (Helps correctly indent Python code)
Summary
This is how easy it is to install Visual Studio Code for Python. You are now all set for Python development. Next up, it’s time to write your first Hello World Program!