Why Virtual Environments?
Every AI project needs different package versions. Project A might need TensorFlow 2.10, while Project B needs TensorFlow 2.15. Without virtual environments, these conflict and break everything. Virtual environments create isolated spaces where each project has its own dependencies.
The Problem Without Virtual Environments
❌ Global Python (BAD):
- All projects share the same packages
- Upgrading one package breaks other projects
- Can't test different versions
- Dependency hell is inevitable
✅ Virtual Environments (GOOD):
- Each project has its own isolated packages
- Upgrade freely without breaking other projects
- Test different versions side-by-side
- Clean, reproducible setups
Option 1: venv (Built-in, Lightweight)
venv comes with Python 3.3+. It's simple, fast, and perfect for most AI projects.
Create a Virtual Environment
cd ~/projects/my-ai-project
python3 -m venv venv
This creates a venv/ folder in your project directory with isolated Python and packages.
Activate the Environment
source venv/bin/activate
You'll see (venv) in your terminal prompt. Now all pip install commands install into this environment only.
Install Packages
pip install langchain openai transformers
These packages are now isolated to this project only.
Deactivate When Done
deactivate
Returns you to the global Python environment.
Option 2: Conda (For Data Science & ML)
Conda is a more powerful environment manager. It handles non-Python dependencies (like CUDA for GPU) and is popular in data science.
Install Miniconda (Lightweight Conda)
brew install --cask miniconda
Create a Conda Environment
conda create -n my-ai-env python=3.11
conda activate my-ai-env
Install Packages with Conda
conda install numpy pandas scikit-learn
pip install langchain openai # Can use pip too!
Deactivate
conda deactivate
venv vs conda: Which to Use?
| Feature | venv | conda |
|---|---|---|
| Built-in | ✅ Yes (Python 3.3+) | ❌ Install separately |
| Speed | ✅ Fast, lightweight | ⚠️ Slower, heavier |
| Non-Python packages | ❌ Python only | ✅ Yes (CUDA, R, etc.) |
| Best for | General AI projects | Data science, ML with GPU |
Recommendation: Start with venv. Switch to conda if you need GPU support or specific data science packages.
Best Practices
1. One Environment Per Project
Never share environments between projects. Create a new one for each.
2. Use requirements.txt
Save your dependencies so you (or others) can recreate the environment:
pip freeze > requirements.txt # Save dependencies
pip install -r requirements.txt # Restore later
3. Add venv to .gitignore
Never commit virtual environments to Git—they're large and platform-specific:
echo "venv/" >> .gitignore
echo ".env/" >> .gitignore
echo "__pycache__/" >> .gitignore
4. Name Environments Clearly
Use descriptive names: langchain-chatbot, image-gen-sd, etc.
Quick Start Example
Here's a complete workflow for a new AI project:
# Create project folder
mkdir ~/projects/ai-chatbot
cd ~/projects/ai-chatbot
# Create virtual environment
python3 -m venv venv
# Activate it
source venv/bin/activate
# Install AI packages
pip install langchain openai python-dotenv
# Create requirements file
pip freeze > requirements.txt
# Work on your project...
# When done, deactivate
deactivate