Virtual Environments

Learn about Python virtual environments, why they're important, and how to create, activate, and manage them.

Virtual Environments

A virtual environment is an isolated Python environment that allows you to install packages separately for different projects. This prevents conflicts between project dependencies.

Why Use Virtual Environments?

  1. Dependency Isolation: Each project has its own packages
  2. Version Control: Different projects can use different package versions
  3. Clean System: Keeps your system Python installation clean
  4. Reproducibility: Easy to recreate project environments
  5. No Conflicts: Avoids package version conflicts

Creating a Virtual Environment

Terminal window
python -m venv myenv

This creates a directory myenv with:

  • Python interpreter
  • pip
  • Standard library
  • Site-packages directory

Using virtualenv (Alternative)

Terminal window
pip install virtualenv
virtualenv myenv

Activating a Virtual Environment

Windows

Terminal window
myenv\Scripts\activate

Linux/Mac

Terminal window
source myenv/bin/activate

When activated, your prompt will show the environment name:

Terminal window
(myenv) C:\Users\YourName>

Deactivating a Virtual Environment

Terminal window
deactivate

Working with Virtual Environments

Install Packages in Virtual Environment

Terminal window
# Activate first
source myenv/bin/activate # Linux/Mac
# or
myenv\Scripts\activate # Windows
# Then install packages
pip install requests
pip install numpy

List Installed Packages

Terminal window
pip list

Create Requirements File

Terminal window
pip freeze > requirements.txt

Install from Requirements

Terminal window
pip install -r requirements.txt

Virtual Environment Best Practices

  1. One per project: Create a separate virtual environment for each project
  2. Name clearly: Use descriptive names like project_name_env
  3. Add to .gitignore: Don’t commit virtual environments to git
  4. Document dependencies: Always maintain requirements.txt
  5. Recreate when needed: If environment gets corrupted, recreate it

Project Structure with Virtual Environment

my_project/
.gitignore # Excludes venv/
requirements.txt
README.md
venv/ # Virtual environment (not committed)
src/
my_code.py
tests/
test_code.py

.gitignore Example

.gitignore
# Virtual environments
venv/
env/
ENV/
.venv
# Python cache
__pycache__/
*.pyc
*.pyo
*.pyd
# Requirements
# Keep requirements.txt, ignore others

Recreating a Virtual Environment

If you need to recreate an environment:

Terminal window
# Remove old environment
rm -rf myenv # Linux/Mac
rmdir /s myenv # Windows
# Create new environment
python -m venv myenv
# Activate
source myenv/bin/activate # Linux/Mac
myenv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt

Virtual Environment Tools

venv (Built-in)

Terminal window
python -m venv myenv

virtualenv

Terminal window
pip install virtualenv
virtualenv myenv

conda (Anaconda)

Terminal window
conda create -n myenv python=3.9
conda activate myenv

Common Workflow

  1. Create project directory

    Terminal window
    mkdir my_project
    cd my_project
  2. Create virtual environment

    Terminal window
    python -m venv venv
  3. Activate virtual environment

    Terminal window
    source venv/bin/activate # Linux/Mac
    venv\Scripts\activate # Windows
  4. Install packages

    Terminal window
    pip install package_name
  5. Create requirements.txt

    Terminal window
    pip freeze > requirements.txt
  6. Work on project

    Terminal window
    python my_script.py
  7. Deactivate when done

    Terminal window
    deactivate

Troubleshooting

Virtual Environment Not Activating

Check Python installation:

Terminal window
python --version
which python # Linux/Mac
where python # Windows

Packages Not Found After Activation

Make sure virtual environment is activated:

Terminal window
which python # Should show venv path

Permission Errors

On Linux/Mac, you might need to set execute permissions:

Terminal window
chmod +x myenv/bin/activate

Exercises

Exercise 1: Virtual Environment Commands

Write the commands to: 1) Create a virtual environment named ‘myenv’, 2) Activate it (show both Windows and Linux/Mac versions), 3) Install a package ‘requests’, 4) Deactivate. Print these commands as a guide.

Virtual Environment Commands

Checks: 0 times
Answer:
commands = """1. python -m venv myenv
2a. myenv\\Scripts\\activate (Windows)
2b. source myenv/bin/activate (Linux/Mac)
3. pip install requests
4. deactivate"""
print(commands)

Exercise 2: Requirements Workflow

After activating a virtual environment and installing packages, write the command to create a requirements.txt file. Then show how to install from it in a new environment.

Requirements Workflow

Checks: 0 times
Answer:
print("Create: pip freeze > requirements.txt")
print("Install: pip install -r requirements.txt")

Course Progress

Section 61 of 61

Back to Course