How to Run Python in VSCode (Visual Studio Code) Tutorial

In this tutorial, we are going to learn how to get started using VSCode with Python.

This tutorial is not intended to teach you Python. If you are looking to learn python, read my beginner guide to learn Python programming.

We are going to learn:


Subscribe to my Newsletter


What is VSCode?

VSCode, or Visual Studio Code, is a free and open-source code editor built by Microsoft used by web developers to build and debug web applications.

Technology: VSCode works with Windows, Linux and macOS.

Popular alternatives to VSCode: Competitor products of VSCode are Notepad++, Sublime Text, PyCharm, and Atom.

Why Use VSCode?

Visual studio code is free, super-fast and supports a lot of programming languages. It has a lot of exciting features:

  • Integrated terminal
  • Built-in Git Integration. (see how to use Git in VSCode)
  • Debugging.
  • Syntax highlighting.
  • Intellisense. Intelligent code completion.

When all else fails, you can download VSCode Extensions.

VSCode is probably not the best Python IDE in the market, but combine VSCode with the Python extension and you have a very powerful Python IDE.

Install VSCode

Install VSCode on Windows, Mac or Linux.

You can install VSCode using the executable file from the visual studio code website.

Alternatively, ff you are using Mac, you can install VSCode using Homebrew.

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
$ brew install --cask visual-studio-code

Install the Python Extension

Now, install the Python extension for VS Code from the Visual Studio Marketplace.

See the extension features later in this post to know why it is recommended to install this extension.

Create a Python Script in VSCode

Now, it is time to create your first python script in VSCode.

You can do this in two ways: from the user interface of from terminal.

From the UI

To create a python script from the Visual Studio Code user interface, go to the explorer on the left hand side of the screen and click on the file with a plus (+) sign.

Give your file a name ending with .py.

From the Terminal

Go to Terminal > New Terminal.

and run $ touch your_script.py.

You can also open a file in VSCode from the terminal using the code command.

Run Python Code

There are 3 ways to run python code in vscode: from the terminal, from the python interactive window or from a notebook.

1. Run Python from Terminal

To run python code from terminal inside VSCode, you don’t need the Python extension.

Go to Terminal > New Terminal.

Run

$ python your_script.py

2. Run Python with the Python Interactive

To run a script line by line using IPython, you need to have the Python extension installed.

To run code line by line with IPython, select the line then Press Shift + Return.

You can also right + click.

3. Run a Python Notebook in VSCode

With the Python extension, you can also run a Python Notebook in VSCode.

Create a file with the .ipynb extension.

Run your code line-by-line by pressing the green “play” sign.

Create a Virtual Environment

This command creates a virtual environment. Note that you can also use Anaconda Environments instead.

L’attribut alt de cette image est vide, son nom de fichier est image-31-1024x495.png.

Activate it by running the activate file.

L’attribut alt de cette image est vide, son nom de fichier est image-32-1024x202.png.

To deactivate the environment, use deactivate.

VSCode Python Extension Features

Code Snippets

The Python extension comes with code snippets.

Autocomplete

Properties in a Library

Once you have imported a library, you will see the methods inside that library.

Environment Selector

If you click on the Python version at the bottom.

You will see that the Python extension lets you select the Python environment that you want.

Other Interesting Python Extensions for VSCode

Python Docstring Generator

Python Docstring Generator is super helpful to generate docstrings for python functions.

  • Press enter after opening docstring with triple quotes (""" or ''')
  • Keyboard shortcut: ctrl+shift+2 or cmd+shift+2 for mac

Conclusion

This is it for this introduction to setting up VSCode for Python programming.

If you are interested in more Python tutorials, head over to my complete guide on Python for SEO.

4.2/5 - (5 votes)