Why isolation matters
Imagine you are maintaining two Python projects simultaneously:- Project A depends on version
1.0of a library - Project B requires version
2.0of the same library, which changed its API
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:
- A copy of the Python interpreter
- A
site-packagesdirectory where installed packages live - Activation scripts that switch your shell to use this environment
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.Method 1: VS Code Command Palette (recommended for beginners)
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.
.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).(.venv) at the start of your terminal prompt:
Manually activating from the terminal
If you ever need to activate the environment in a terminal outside of VS Code, here are the commands:Troubleshooting
Command not found: python -m venv
Command not found: python -m venv
On Ubuntu or Debian, the On other systems it is included with Python by default.
venv module is sometimes distributed separately:Permission denied on activation
Permission denied on activation
On macOS/Linux, make the activation script executable:
VS Code does not recognise the virtual environment
VS Code does not recognise the virtual environment
- Press
Ctrl/Cmd + Shift + P→ Developer: Reload Window - Then press
Ctrl/Cmd + Shift + P→ Python: Select Interpreter again - 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.