Virtual environments
Keep your Python projects organized
Your Python installation
Remember when we installed Python? That installation lives in a specific folder on your computer:
- Windows:
C:\Users\[YourName]\AppData\Local\Programs\Python\Python313\ - macOS:
/Library/Frameworks/Python.framework/Versions/3.13/ - Linux:
/usr/bin/python3
This is your “system Python” - it’s shared by everything on your computer.
The problem with sharing
Imagine you’re working on two projects:
- Project A needs version 1.0 of a tool
- Project B needs version 2.0 of the same tool
If you install version 2.0 for Project B, you just broke Project A! This is where virtual environments save the day.
What are virtual environments?
A virtual environment is like a private copy of Python for each project. Think of it as:
- A separate folder for each project
- Its own Python installation
- Its own place to install packages
- Complete isolation from other projects
Virtual environments are so important that I create a new one for EVERY project. It’s a Python best practice that will save you from many headaches.
Understanding packages
Before we continue, let’s understand what packages are:Packages are pre-written code that others have created for us to use. Instead of writing everything from scratch, we can import these packages. For example:
requests- for downloading web pagespandas- for working with dataopenai- for using AI models
Each package has different versions, and different projects might need different versions.
Create your first virtual environment
Let’s create one for our python-for-ai project. You have two methods:
Method 1: VS Code Command Palette (easier)
- Open your project in VS Code
- Press
Ctrl/Cmd + Shift + P - Type “Python: Create Environment”
- Select “Venv”
- Select your Python installation
- VS Code creates and activates everything for you!
Method 2: Terminal command
- Open the terminal in VS Code (View > Terminal)
- Make sure you’re in your project folder
- Run this command:
python -m venv .venvWhat this does:
python -m venv- Run Python’s virtual environment module.venv- The name of the folder to create (convention is to use “.venv” with a dot)
What just happened?
Look at your project folder - you now have a new .venv folder:
python-for-ai/
├── .venv/ # Your virtual environment (hidden folder)
├── hello.py # Your code
└── python-for-ai.code-workspaceThe dot in .venv makes it a hidden folder on Mac/Linux. This keeps your project clean since you don’t need to see this folder often.
This .venv folder contains:
- A copy of Python
- A place to install packages
- Scripts to activate/deactivate
Activate your virtual environment
VS Code makes activation super easy:
- Press
Ctrl/Cmd + Shift + Pto open Command Palette - Type “Python: Select Interpreter”
- Choose the one that says
.venv(it will show the full path like./.venv/bin/python) - That’s it! VS Code automatically activates it for you
You’ll know it’s working when you see (.venv) in your terminal:
(.venv) PS C:\...\python-for-ai>This is my recommended approach. VS Code remembers your choice and automatically activates the environment every time you open the project!
Common issues
Command not found
If python -m venv doesn’t work, you might need to install it:Ubuntu/Debian:
sudo apt install python3-venv
Other systems: It should come with Python by default
Permission denied
On macOS/Linux, you might need to make the activate script executable:
chmod +x .venv/bin/activate
VS Code not recognizing venv
- Reload VS Code window:
Ctrl/Cmd + Shift + P> “Developer: Reload Window” - Manually select interpreter again
- Make sure the Python extension is installed
What about Anaconda?
You might have heard of Anaconda - it’s another tool that manages Python environments and packages. It’s popular in the data science world because it comes pre-loaded with many data science packages.However, I don’t recommend using Anaconda unless you have a specific reason to.
Stick with Python’s built-in virtual environments unless your company or a specific project requires Anaconda. You’ll have a lighter, faster, and simpler setup.
What’s next?
Your virtual environment is ready! Now let’s learn about Python packages and how to install them.