Visual Studio Code (VSCode) is one of the most popular code editors at the moment. It is a lightweight editor with an extensive community of developers.
One of the advantages of VSCode is that it supports a wide variety of languages. Among them, of course, is Python (in fact, it is one of the best environments we can use for programming in Python).
So let’s see how to install and, above all, how to configure VSCode for Python development.
If you want to learn more about Visual Studio Code
check out the Introduction to Programming Course read more
Installing Visual Studio Code
If you don’t have Visual Studio Code installed on your system yet, follow these steps to download and install the editor:
- Open your web browser and go to the official Visual Studio Code website.
- Click the download button for your operating system (Windows, macOS, or Linux).
- Once the download is complete, run the installer and follow the instructions to complete the installation.
Configuring VSCode for Python
To enable Python support in VSCode, we need to install the official Python extension.
To do this,
- Open Visual Studio Code
- Go to the Extensions tab in the left sidebar (four squares icon).
- In the search field, type Python
- You should see the “Python” extension offered by Microsoft in the search results.
- Click on Install to install the extension
Running Python scripts
Now we are ready to create and run a Python script in Visual Studio Code. First, it is convenient to create a folder for your project and open it with VSCode.
You can do this either with the file explorer or with the command console by executing
mkdir mi_proyecto
cd mi_proyecto
Inside our folder, create a new Python file by clicking on File > New File (or using the keyboard shortcut (Ctrl + N).
For this example, we can name the file anything, for example, hola.py
. Open your file, write the following, and save the file.
print("Hello from LuisLlamas.es")
Now, to run the Python script, we can click on the play button in the upper right corner of the editor.
You will see the output of your script in the VSCode output window.
Hello from LuisLlamas.es
How to debug
Finally, we can also use Visual Studio Code to debug our code in Python.
To do this, with a Python file open, click on Run > Start debugging or use the keyboard shortcut (F5).
A window will pop up for you to choose the debugger you want to use. In this case, select Python Debugger.
Next, it will ask for the Debug configuration. For now, choose Python File.
Now you can run your code line by line, use breakpoints, or use the debug tools to inspect the value of your variables while the program runs.