RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Python for AI — Zero to Useful
  6. /Ch. 28
Python for AI — Zero to Useful

28. Virtual Environments Deep Dive

Chapter 28 of 36 · 15 min
KEY INSIGHT

Create a new virtual environment for every project. Never install packages globally unless you're sure you need system-wide availability. When you need to reproduce an environment: `pip install -r requirements.txt`. A practical addition: `venv` with `.python-version` and auto-activation using direnv or shell integration: ```bash # .envrc for direnv (if using direnv) layout python python # Or in .bashrc/.zshrc, add git-aware prompt modification # that auto-activates when entering directories with .venv ```

Virtual environments isolate project dependencies. Without them, conflicting package versions break things. With them, each project gets its own clean Python environment.

The modern approach uses venv (built into Python 3.3+) or virtualenv (more mature, faster):

# Create a virtual environment
python -m venv myenv

# Activate it (Linux/macOS)
source myenv/bin/activate

# Activate it (Windows PowerShell)
myenv\Scripts\Activate.ps1

# Verify activation
which python  # Should point to myenv/bin/python
python --version

# Install packages
pip install openai numpy pandas

# Freeze requirements
pip freeze > requirements.txt

# Deactivate when done
deactivate

The python -m venv approach is now Python's officially recommended method. Older tutorials may show pyenv, virtualenvwrapper, or other tools—these predate the venv module.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Create a new virtual environment called test_env. Activate it, install numpy and requests, verify they're importable, create requirements.txt, then deactivate and delete the environment. Demonstrate you can recreate it from requirements.txt in a new location.

← Chapter 27
Optimizing Python Code
Chapter 29 →
Dependency Management