Language: EN

como-usar-python-con-vscode

How to use Visual Studio Code with Python

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.

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:

  1. Open your web browser and go to the official Visual Studio Code website.
  2. Click the download button for your operating system (Windows, macOS, or Linux).
  3. 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.

vscode-python-extension

To do this,

  1. Open Visual Studio Code
  2. Go to the Extensions tab in the left sidebar (four squares icon).
  3. In the search field, type Python
  4. You should see the “Python” extension offered by Microsoft in the search results.
  5. 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.

vscode-python-example

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.

vscode-select-python-debugger

Next, it will ask for the Debug configuration. For now, choose Python File.

vscode-select-debug-configuration

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.

vscode-python-debug