Why Virtual Environments?

Different projects need different package versions. Project A might need requests 2.28 while Project B needs requests 2.31. Without isolation, installing one breaks the other.

Virtual environments create isolated Python installations per project, each with its own packages. This is a fundamental best practice — every serious Python project uses one.

⚠️
Never install packages globally

Running pip install without a virtual environment installs packages system-wide, which can break your OS tools and conflict between projects. Always activate a venv first.

Creating a Virtual Environment

1
Create the virtual environment:
python3 -m venv myproject-env

This creates a myproject-env/ directory containing an isolated Python installation.

2
Activate it:
# Linux / macOS
source myproject-env/bin/activate

# Windows
myproject-env\Scripts\activate

Your prompt changes to show the active environment: (myproject-env) $

3
Deactivate when done:
deactivate
💡
Common convention

Most developers name their virtual environment venv or .venv and create it inside the project directory. Add it to .gitignore — never commit the venv folder to version control.

Installing Packages with pip

# Install a package
pip install requests

# Install a specific version
pip install requests==2.31.0

# Install minimum version
pip install "requests>=2.28"

# Upgrade a package
pip install --upgrade requests

# Uninstall a package
pip uninstall requests

# Show installed packages
pip list

# Show details about a package
pip show requests

requirements.txt

A requirements.txt file lists all packages your project needs, making it easy for others (or your future self) to recreate the environment:

Creating requirements.txt

# Export current packages to requirements.txt
pip freeze > requirements.txt

The file looks like:

certifi==2024.2.2
charset-normalizer==3.3.2
idna==3.6
requests==2.31.0
urllib3==2.2.1

Installing from requirements.txt

# Install all packages from requirements.txt
pip install -r requirements.txt
💡
Hand-written vs frozen requirements

pip freeze includes every sub-dependency with exact versions. For simpler projects, you can hand-write requirements.txt with just your direct dependencies: requests>=2.28

Project Setup Workflow

Here's the standard workflow for starting a new Python project:

# 1. Create project directory
mkdir my-project && cd my-project

# 2. Create virtual environment
python3 -m venv .venv

# 3. Activate it
source .venv/bin/activate

# 4. Install packages you need
pip install requests python-dotenv

# 5. Save dependencies
pip freeze > requirements.txt

# 6. Create .gitignore
echo ".venv/" > .gitignore
echo "__pycache__/" >> .gitignore
echo "*.pyc" >> .gitignore
echo ".env" >> .gitignore

# 7. Start coding!
touch main.py

Cloning Someone Else's Project

# 1. Clone the repository
git clone https://example.com/project.git
cd project

# 2. Create your own virtual environment
python3 -m venv .venv
source .venv/bin/activate

# 3. Install the project's dependencies
pip install -r requirements.txt

# 4. Run the project
python main.py

Popular Python Packages

  • requests — HTTP requests (API calls, web scraping)
  • flask — Lightweight web framework
  • python-dotenv — Load environment variables from .env files
  • pytest — Testing framework
  • black — Code formatter
  • rich — Beautiful terminal output (colors, tables, progress bars)
  • click — Build command-line interfaces
  • pillow — Image processing

Useful pip Commands Reference

# Search for packages (use pypi.org for browsing)
pip search package-name        # Deprecated, use website

# Check for outdated packages
pip list --outdated

# Install from a Git repository
pip install git+https://github.com/user/repo.git

# Install in development/editable mode
pip install -e .

# Check for dependency conflicts
pip check

# Show where packages are installed
pip show -f requests

Common Pitfalls

⚠️
Don't commit your venv folder

Virtual environments contain thousands of files specific to your OS and Python version. Always add .venv/ to .gitignore and share requirements.txt instead.

# .gitignore for Python projects
.venv/
__pycache__/
*.pyc
*.pyo
.env
*.egg-info/
dist/
build/

Summary

  • Virtual environments isolate project dependencies: python3 -m venv .venv
  • Activate with source .venv/bin/activate, deactivate with deactivate
  • pip install package installs packages; pip freeze > requirements.txt saves them
  • pip install -r requirements.txt recreates an environment from a file
  • Never install packages globally or commit the venv folder
  • Every Python project should have a virtual environment and a requirements.txt
🎉
Package management mastered!

You now have all the tools to manage Python projects professionally. With basics, intermediate skills, and proper tooling under your belt, you're ready to build real-world Python applications!