Python Environment Setup (venv, pip, poetry)
Set Up Your AI/ML Development Environment Like a Pro
Learn how to manage Python environments, dependencies, and tools so your AI projects never face 'it works on my machine' problems.
Why Python Environments Matter
The Dependency Hell Problem
The Core Problem:
Imagine you have two AI projects. Project A needs TensorFlow 2.12 and Project B needs TensorFlow 2.16. If you install both globally, one project will break. This is called dependency hell.
Virtual environments solve this by giving each project its own isolated Python installation with its own packages. What happens in one environment stays in that environment.
Real-World Analogy - Swiggy Cloud Kitchens:
Think of Python environments like Swiggy cloud kitchens. Each kitchen (environment) has its own ingredients (packages), equipment (Python version), and recipes (code). Even though they are in the same building (your computer), they do not interfere with each other.
Without environments, it is like all chefs sharing one kitchen - someone needs sugar for dessert while another needs the counter for biryani. Chaos!
What We Will Cover:
- venv: Python's built-in virtual environment tool (simplest)
- pip: The default package installer for Python
- poetry: Modern dependency management with lock files (best for serious projects)
- conda: Popular in data science for managing Python versions + system libraries
Note: Rule #1 of Python development: NEVER install packages globally. Always use a virtual environment. This saves you hours of debugging broken installations.
venv - Python's Built-in Virtual Environments
The Simplest Way to Isolate Your Projects
What is venv?
venv is Python's built-in module (since Python 3.3) for creating lightweight virtual environments. No extra installation needed - it comes with Python.
When you create a venv, Python copies its interpreter and creates an isolated site-packages directory where pip installs packages.
How to Use venv - The Essential Commands:
# Create a virtual environment
python -m venv myproject-env
# Activate it (macOS/Linux)
source myproject-env/bin/activate
# Activate it (Windows)
myproject-envScriptsactivate
# Your terminal shows (myproject-env) prefix
# Now pip install goes into this environment only
# Install packages
pip install langchain openai
# Save dependencies
pip freeze > requirements.txt
# Deactivate when done
deactivate
# Recreate environment from requirements
pip install -r requirements.txt
venv Best Practices:
- Name convention: Use .venv or venv in your project root
- Git ignore: Always add your venv folder to .gitignore
- requirements.txt: Always maintain this file for reproducibility
- One venv per project: Never share environments between projects
venv Limitations:
- Cannot manage different Python versions (you need pyenv for that)
- requirements.txt does not lock transitive dependencies properly
- No built-in way to separate dev vs production dependencies
- pip freeze captures everything, including unused packages
Note: venv + pip is perfect for quick experiments and learning. For production AI projects, consider poetry or conda for better dependency management.
pip - Python Package Management
Installing, Managing, and Tracking Python Packages
What is pip?
pip (Pip Installs Packages) is Python's default package manager. It downloads packages from PyPI (Python Package Index) - the central repository with 500,000+ packages.
Think of PyPI like the Play Store for Python libraries. pip is the app that downloads and installs from it.
Essential pip Commands for AI/ML:
# Install a specific version
pip install transformers==4.38.0
# Install with version constraints
pip install "langchain>=0.1.0,<0.2.0"
# Install from requirements file
pip install -r requirements.txt
# Show installed packages
pip list
# Show details of a package
pip show torch
# Upgrade a package
pip install --upgrade openai
# Uninstall
pip uninstall tensorflow
# Install extra dependencies
pip install "langchain[all]"
requirements.txt - The Dependency File:
# requirements.txt - Pin exact versions for reproducibility
openai==1.12.0
langchain==0.1.9
chromadb==0.4.22
python-dotenv==1.0.0
fastapi==0.109.0
uvicorn==0.27.0
Pro tip: Always pin exact versions in production. Use >= only during development when you want the latest.
Common AI/ML Packages You Will Use:
| Package | Purpose |
|---|---|
| openai | OpenAI API client (GPT, DALL-E) |
| anthropic | Claude API client |
| langchain | LLM application framework |
| transformers | Hugging Face models |
| torch | PyTorch deep learning |
| chromadb | Vector database |
| fastapi | API server for AI apps |
Note: PyTorch (torch) is a large package (~2GB). Always install it in a virtual environment, never globally. Use the PyTorch website to get the correct install command for your GPU.
Poetry - Modern Python Dependency Management
The Best Tool for Serious Python Projects
Why Poetry Over pip?
Poetry solves the problems pip does not handle well:
- Lock file: poetry.lock captures exact versions of ALL dependencies (including transitive ones). pip freeze is not reliable for this.
- Dev dependencies: Separate dev tools (pytest, black) from production deps
- Dependency resolution: Checks for version conflicts BEFORE installing
- Project metadata: Single pyproject.toml for everything
- Virtual env management: Creates and manages venvs automatically
Poetry Workflow:
# Install poetry (one-time)
curl -sSL https://install.python-poetry.org | python3 -
# Create new project
poetry new my-ai-project
# Or initialize in existing project
poetry init
# Add dependencies
poetry add langchain openai chromadb
# Add dev dependencies
poetry add --group dev pytest black ruff
# Install all dependencies
poetry install
# Run commands inside the environment
poetry run python main.py
# Activate shell
poetry shell
# Update dependencies
poetry update
# Export to requirements.txt (for Docker)
poetry export -f requirements.txt --output requirements.txt
pyproject.toml - Poetry's Config File:
[tool.poetry]
name = "my-ai-project"
version = "0.1.0"
description = "AI-powered Flipkart review analyzer"
[tool.poetry.dependencies]
python = "^3.11"
langchain = "^0.1.9"
openai = "^1.12.0"
chromadb = "^0.4.22"
[tool.poetry.group.dev.dependencies]
pytest = "^8.0"
black = "^24.1"
ruff = "^0.2.0"
Poetry vs pip - When to Use Which:
| Scenario | Use |
|---|---|
| Quick experiment / learning | venv + pip |
| Serious project / team work | Poetry |
| Data science / Jupyter heavy | Conda |
| Library publishing to PyPI | Poetry |
| Docker deployment | Poetry (export to requirements.txt) |
Note: For AI projects with many dependencies (LangChain, vector DBs, API clients), Poetry is strongly recommended. The lock file ensures everyone on the team has the exact same setup.
Setting Up a Complete AI Project Environment
Step-by-Step: From Zero to AI-Ready
Step 1: Install Python 3.11+
Download from python.org or use a version manager:
# macOS with Homebrew
brew install python@3.11
# Or use pyenv for multiple versions
brew install pyenv
pyenv install 3.11.8
pyenv global 3.11.8
# Verify
python --version # Python 3.11.8
Step 2: Project Setup with Poetry
# Create project
mkdir flipkart-review-bot && cd flipkart-review-bot
poetry init --name flipkart-review-bot --python "^3.11"
# Add AI dependencies
poetry add langchain openai chromadb python-dotenv
# Add dev tools
poetry add --group dev pytest ruff
# Create project structure
mkdir -p src tests
touch src/__init__.py src/main.py
touch .env .gitignore
Step 3: Environment Variables (.env)
# .env - NEVER commit this file!
OPENAI_API_KEY=sk-proj-...
ANTHROPIC_API_KEY=sk-ant-...
# .gitignore
.env
.venv/
__pycache__/
*.pyc
.ruff_cache/
CRITICAL: Never commit API keys to git. Use .env files + python-dotenv to load them securely.
Step 4: VS Code Setup for AI Development
- Python extension: Provides IntelliSense, debugging, linting
- Jupyter extension: Run notebooks inside VS Code
- Select interpreter: Cmd+Shift+P > "Python: Select Interpreter" > choose your venv
- Ruff extension: Fast Python linter (replaces flake8, isort, black)
Note: API keys are the most common security mistake. Always use .env files, always add .env to .gitignore, and never hardcode keys in your source code.
Common Environment Pitfalls & Fixes
Problems You WILL Encounter (And How to Fix Them)
Problem 1: "Module not found" even after pip install
Cause: You installed the package in global Python but your terminal is using a different venv (or vice versa).
Fix: Check which Python is active: which python. Make sure it points to your venv.
Problem 2: torch/tensorflow fails to install
Cause: These are huge packages with platform-specific builds. Wrong OS/GPU version breaks things.
Fix: Always use the official install command from pytorch.org or tensorflow.org. For Mac M1/M2, use pip install torch torchvision torchaudio (MPS backend).
Problem 3: Version conflicts between packages
Cause: Package A needs numpy<1.24 but package B needs numpy>=1.25.
Fix: Use Poetry - it detects conflicts before installing. With pip, you get cryptic errors after installation.
Problem 4: Accidentally committed .env with API keys
Cause: Forgot to add .env to .gitignore before first commit.
Fix: (1) Immediately revoke and rotate the API key. (2) Remove from git history using git filter-branch or BFG Repo-Cleaner. (3) Add to .gitignore. The key is already exposed even if you delete the file - ALWAYS rotate.
Problem 5: Different behavior on teammate's machine
Cause: Different package versions, OS, or Python version.
Fix: Use poetry.lock (or requirements.txt with pinned versions). Use Docker for complete reproducibility. Specify Python version in pyproject.toml.
Note: 90% of 'it does not work on my machine' issues come from environment problems. Invest time in proper setup once, save hours of debugging later.
Interview Questions
Q: Why should you use virtual environments in Python?
Virtual environments isolate project dependencies to avoid conflicts. Different projects may need different versions of the same package. Without venvs, installing a package for one project can break another. Each venv has its own Python interpreter and site-packages directory.
Q: What is the difference between pip freeze and a poetry.lock file?
pip freeze lists currently installed packages but does not resolve transitive dependencies reliably, and may include unrelated packages. poetry.lock is a complete, deterministic snapshot of ALL dependencies (including transitive), with hashes for integrity. Poetry's lock file guarantees exact reproducibility.
Q: How do you manage API keys securely in a Python AI project?
(1) Store keys in .env file, never in source code. (2) Add .env to .gitignore BEFORE first commit. (3) Use python-dotenv to load them. (4) In production, use environment variables or secrets managers (AWS Secrets Manager, HashiCorp Vault). (5) If keys are accidentally committed, rotate them immediately.
Q: When would you choose conda over pip/poetry?
Conda is preferred for data science/ML when you need: (1) system-level libraries (CUDA, MKL, OpenBLAS), (2) multiple Python versions managed together, (3) non-Python dependencies (R, Julia). pip/poetry are better for pure Python projects, web apps, and when you want tighter dependency resolution.
Q: How do you ensure reproducible environments across a team?
(1) Use poetry.lock or pinned requirements.txt committed to git. (2) Specify Python version in pyproject.toml. (3) Use Docker for complete OS-level reproducibility. (4) Document setup steps in README. (5) Use CI/CD to validate the environment builds correctly.
Frequently Asked Questions
What is Python Environment Setup?
Learn how to manage Python environments, dependencies, and tools so your AI projects never face 'it works on my machine' problems.
How does Python Environment Setup work?
The Dependency Hell Problem The Core Problem: Imagine you have two AI projects. Project A needs TensorFlow 2.12 and Project B needs TensorFlow 2.16 .
Related topics
Practice this on DevInterviewMaster
Read the full Python Environment Setup (venv, pip, poetry) breakdown with interactive demos, quizzes, and Hinglish notes.
800+ system-design, LLD, coding, and design-pattern topics. Unlock everything with Pro (₹499, one-time) or Ultimate (₹999, one-time) — lifetime access, no subscription.