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 is compatible with a wide variety of languages. Among them, of course, is Python. In fact, it is one of the best environments we can use to program 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 Function Overloading
check 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:

  1. Open a web browser and go to the official Visual Studio Code website.
  2. Click on 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 on 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 “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.

Now we create a new Python file by clicking on File > New File or using the keyboard shortcut (Ctrl + N). For this example, we can call the file anything, for example, hello.py.

We open our 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 Python code.

To do this with an open Python file, click on Run > Start debugging or use the keyboard shortcut (F5).

A window will appear to choose the debugger you want to use. In this case, we choose “Python Debugger”.

vscode-select-python-debugger

Next, it will ask for the Debug configuration. For now, we 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 is running.

vscode-python-debug