Skip to main content
One of the first habits every serious Python developer builds is creating a virtual environment for every project. Without one, all the packages you install across all your projects pile up in a single shared Python installation — and that shared space quickly leads to version conflicts, broken projects, and debugging sessions that are very hard to trace. Virtual environments eliminate this problem entirely by giving each project its own isolated Python instance with its own package directory.

Why isolation matters

Imagine you are maintaining two Python projects simultaneously:
  • Project A depends on version 1.0 of a library
  • Project B requires version 2.0 of the same library, which changed its API
If both projects share the same Python installation, upgrading the library for Project B breaks Project A. There is no clean way to resolve this without isolation. A virtual environment solves this by creating a self-contained copy of Python for each project. Installing a package into Project A’s environment has zero effect on Project B’s environment.
Create a new virtual environment for every Python project you start — no exceptions. This is a universal best practice in professional Python development, and it will save you significant debugging time.

What a virtual environment contains

When you create a virtual environment (conventionally named .venv), Python creates a folder with three things:
  1. A copy of the Python interpreter
  2. A site-packages directory where installed packages live
  3. Activation scripts that switch your shell to use this environment
Your project structure will look like this:
The leading dot in .venv makes it a hidden folder on macOS and Linux. You rarely need to look inside it — just let VS Code manage it.

Create a virtual environment

You have two ways to create a virtual environment. The VS Code method is easier; the terminal method is useful when you need more control.
1

Open the Command Palette

Press Ctrl/Cmd + Shift + P.
2

Create the environment

Type Python: Create Environment and press Enter.
3

Choose Venv

Select Venv when asked for the environment type.
4

Select your Python installation

Choose the Python version you installed earlier.
VS Code creates the .venv folder and automatically selects it as the interpreter for your project.

Method 2: Terminal command

1

Open the terminal

Press Ctrl + ` (Windows/Linux) or Cmd + ` (macOS), or go to View → Terminal.
2

Navigate to your project folder

Make sure you are inside python-for-ai/ before running the next command.
3

Create the environment

Activate the virtual environment in VS Code

Creating the environment is only half the job — you also need to make VS Code use it.
1

Open the Command Palette

Press Ctrl/Cmd + Shift + P.
2

Select the interpreter

Type Python: Select Interpreter and press Enter.
3

Choose .venv

Select the interpreter that shows .venv in its path (something like ./.venv/bin/python).
Once selected, VS Code activates the environment automatically every time you open a new terminal in this project. You will see (.venv) at the start of your terminal prompt:
This is the recommended workflow. VS Code remembers your interpreter choice per workspace, so you never have to reactivate manually — just open the project and the environment is already active.

Manually activating from the terminal

If you ever need to activate the environment in a terminal outside of VS Code, here are the commands:
To deactivate and return to the system Python:

Troubleshooting

On Ubuntu or Debian, the venv module is sometimes distributed separately:
On other systems it is included with Python by default.
On macOS/Linux, make the activation script executable:
  1. Press Ctrl/Cmd + Shift + PDeveloper: Reload Window
  2. Then press Ctrl/Cmd + Shift + PPython: Select Interpreter again
  3. Confirm the Python and Jupyter extensions are both installed

A note on Anaconda

Anaconda is an alternative Python distribution popular in data science circles. It comes pre-loaded with many scientific packages and includes its own environment manager (conda). Unless your workplace specifically requires Anaconda, stick with standard Python virtual environments. They are lighter, faster to create, and work seamlessly with every tool and tutorial you will encounter in modern Python development.

Python packages

Learn how to install packages with pip inside your virtual environment.