In this tutorial, we are going to learn how to get started using VSCode with Python.
We are going to learn:
- What is VSCode and why use it
- How to install VSCode
- Install the Python Extension
- Create a Python script
- Run Python code in Visual Studio Code
- Create a virtual environment
- Python extension features
- Other interesting VSCode extensions for Python
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)
- 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 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
From the Terminal
Go to Terminal > New Terminal.
$ touch your_script.py.
You can also open a file in VSCode from the terminal using the
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.
$ 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
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
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.
Activate it by running the activate file.
To deactivate the environment, use
VSCode Python Extension Features
The Python extension comes with code snippets.
Properties in a Library
Once you have imported a library, you will see the methods inside that library.
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 (
- Keyboard shortcut:
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.
SEO Strategist at Tripadvisor, ex- Seek (Melbourne, Australia). Specialized in technical SEO. Writer in Python, Information Retrieval, SEO and machine learning. Guest author at SearchEngineJournal, SearchEngineLand and OnCrawl.