Getting Started with Python Virtual Environments

by Pyrastra Team
Getting Started with Python Virtual Environments

Introduction

Virtual environments are one of the most important tools in a Python developer’s toolkit. They allow you to create isolated Python environments for different projects, preventing dependency conflicts and keeping your system Python clean.

Why Use Virtual Environments?

When working on multiple Python projects, you’ll often encounter situations where different projects require different versions of the same package. Without virtual environments, this can lead to:

  • Dependency conflicts: Project A needs Django 3.2, but Project B needs Django 4.0
  • System pollution: Installing packages globally can interfere with system tools
  • Reproducibility issues: Difficult to share exact dependencies with team members

Virtual environments solve these problems by creating isolated Python installations for each project.

Creating a Virtual Environment

Python 3.3+ includes the venv module in the standard library, making it easy to create virtual environments without installing additional tools.

Basic Usage

# Create a virtual environment
python -m venv myenv

# On Windows
myenv\Scripts\activate

# On macOS/Linux
source myenv/bin/activate

Once activated, your terminal prompt will change to show the environment name, indicating you’re working inside the virtual environment.

Installing Packages

With your virtual environment activated, you can install packages using pip:

pip install requests
pip install django==4.0
pip install -r requirements.txt

All packages will be installed only in this virtual environment, not globally.

Managing Dependencies

Creating a Requirements File

To share your project’s dependencies with others:

pip freeze > requirements.txt

This creates a file listing all installed packages and their versions.

Installing from Requirements

Others can recreate your environment using:

pip install -r requirements.txt

Best Practices

1. One Environment Per Project

Create a separate virtual environment for each project:

cd my-project
python -m venv venv

2. Add to .gitignore

Never commit your virtual environment to version control:

# .gitignore
venv/
env/
.venv/

3. Use Descriptive Names

While venv is common, consider using project-specific names:

python -m venv myproject-env

4. Document Python Version

Include the Python version in your README:

## Requirements
- Python 3.10+

Alternative Tools

While venv is built-in and sufficient for most use cases, other tools offer additional features:

virtualenv

More features than venv, works with older Python versions:

pip install virtualenv
virtualenv myenv

conda

Popular in data science, manages both Python and non-Python dependencies:

conda create -n myenv python=3.10
conda activate myenv

pipenv

Combines pip and virtualenv with a Pipfile for dependency management:

pip install pipenv
pipenv install requests
pipenv shell

poetry

Modern dependency management with better resolution:

pip install poetry
poetry init
poetry add requests
poetry shell

Deactivating and Removing

Deactivate

To exit the virtual environment:

deactivate

Remove

To delete a virtual environment, simply remove its directory:

# Windows
rmdir /s venv

# macOS/Linux
rm -rf venv

Common Issues and Solutions

Issue: “python: command not found”

Solution: Use python3 instead of python:

python3 -m venv venv

Issue: Activation script not working on Windows

Solution: You may need to change PowerShell execution policy:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Issue: Wrong Python version in virtual environment

Solution: Specify the Python version explicitly:

python3.10 -m venv venv

Conclusion

Virtual environments are essential for Python development. They keep your projects isolated, dependencies organized, and make collaboration easier. Start using them in every project, and you’ll avoid many common Python headaches.

Next Steps

Happy coding!