Environment Setup
Virtual Environments
3 min read
Virtual environments are isolated Python environments that keep your project's dependencies separate from other projects and your system Python.
Why Use Virtual Environments?
Imagine two AI projects:
- Project A needs
openai==1.0.0 - Project B needs
openai==1.5.0
Without virtual environments, installing one would break the other. Virtual environments solve this by creating isolated spaces for each project.
Creating a Virtual Environment
# Navigate to your project folder
cd my-ai-project
# Create a virtual environment named 'venv'
python3 -m venv venv
This creates a venv folder containing:
- A copy of the Python interpreter
- A place for installed packages
- Activation scripts
Activating the Environment
macOS/Linux
source venv/bin/activate
Windows (Command Prompt)
venv\Scripts\activate.bat
Windows (PowerShell)
venv\Scripts\Activate.ps1
When activated, you'll see (venv) at the start of your terminal prompt.
Deactivating
When you're done working:
deactivate
Best Practices
| Practice | Reason |
|---|---|
| One venv per project | Prevents dependency conflicts |
Name it venv or .venv |
Standard convention, easy to gitignore |
| Don't commit venv to git | It's large and system-specific |
| Recreate from requirements.txt | Share dependencies, not the environment |
Next, we'll learn how to manage packages with pip. :::