How to use Visual Studio Code (VS Code) to set up virtual environment and code in Python— Beginner’s 101 Guide

Henry Wu
4 min readDec 31, 2022

--

This article is for the beginner who wants to use Visual Studio Code (VS Code) to code in Python on Mac or Windows.

2) install the “Python Extension”

① click the “Extensions” icon on the left bar.

② input “python” in the search bar. The one we need is named “Python” and it’s created by Microsoft.

③ click the “install” button.

3) install the Python Interpreter

  • macOS

Open a terminal on macOS and input the following code to install Python.

# macOS
brew install python3
  • Windows

Download Python from the official website and install.

4) Start in a project folder

① click the “Open…” icon on the welcome page.

② create a new folder and click the “Open” button.

③ right-click in the left area and choose “New File…”

④ name the new file “helloworld.py”

5) set up a virtual environment

① open the “Command Palette” of VS Code by shortcut (⌘⇧ + P).

② create a new Terminal by shortcut (control + `) or input “Terminal: Create New Terminal” in the search bar.

③ in the Terminal, input the following code to create a virtual environment named “my_venv” (you can change the name as you please).

NOTICE: The code for macOS and Windows are different:

# macOS
python3 -m venv my_venv
# Windows
py -3 -m venv my_venv

④ in the Terminal, input the following code to activate the virtual environment:

# macOS
source my_venv/bin/activate
# Windows
.venv\scripts\activate
T

The virtual environment is activated when “(my_venv)” is at the beginning of the command line.

⑤ click the “Select Interpreter” button at the lower right corner and click the “my_venv” in the Command Palette.

6) coding

Input the following code in the main area of VS Code:

print("hello world!")

7) run the program

There are several ways to run the program:

① click the triangle in the top left corner.

② use the shortcut (⌘ + return).

③ select the code and right-click, then choose “Run Selection/Line in Python Terminal”.

Welcome to the world of Python!

8) references

Photo by Arnold Francisca on Unsplash

--

--

Henry Wu

Indie Developer/ Business Analyst/ Python/ AI/ Former Journalist/ Codewriter & Copywriter