Skip to content
IB
Back to writing
PythonDeveloper Tools

A Step-by-Step Guide to Creating Python Virtual Environments

A comprehensive guide outlining the essential steps for setting up isolated virtual environments in Python, covering installation, activation, and dependency management.

25 July 2025 · 3 min read

Why Virtual Environments Matter

If you have ever worked on more than one Python project at the same time, you have probably run into dependency conflicts. Project A needs version 1.2 of a library, but Project B needs version 2.0, and they cannot coexist peacefully in the same Python installation.

Virtual environments solve this problem by creating isolated spaces where each project can have its own dependencies, independent of every other project on your machine.

What is a Virtual Environment?

A virtual environment is a self-contained directory that contains a Python installation for a particular version of Python, plus a number of additional packages. When you activate a virtual environment, your shell uses the Python interpreter and packages from that directory instead of the system-wide installation.

Think of it as giving each project its own private Python installation.

Creating a Virtual Environment

Python 3 comes with venv built in, so you do not need to install anything extra. Open your terminal and navigate to your project directory:

python3 -m venv .venv

This creates a .venv directory in your project folder containing the virtual environment. I prefer .venv as a naming convention because the leading dot keeps it hidden in Unix-based file explorers, and most .gitignore templates already exclude it.

Activating the Environment

The activation command depends on your operating system and shell:

macOS and Linux (bash/zsh):

source .venv/bin/activate

Windows (PowerShell):

.venv\Scripts\Activate.ps1

Windows (Command Prompt):

.venv\Scripts\activate.bat

Once activated, you will see the environment name in your terminal prompt, usually as (.venv) at the beginning of the line.

Installing Packages

With the environment active, any packages you install with pip will go into the virtual environment rather than the system Python:

pip install requests flask pandas

To save your dependencies for reproducibility:

pip freeze > requirements.txt

And to install from a requirements file on another machine or after a fresh clone:

pip install -r requirements.txt

Deactivating

When you are done working on the project, simply run:

deactivate

Your terminal will return to using the system Python installation.

Best Practices

  1. Always use virtual environments for every Python project, no matter how small
  2. Add .venv/ to your .gitignore to keep the environment out of version control
  3. Keep requirements.txt updated and commit it to your repository
  4. Use descriptive names if you maintain multiple environments, though .venv works well for single-environment projects
  5. Consider pip-tools for more sophisticated dependency management with pinned versions

Alternatives

While venv is the standard library solution, there are alternatives worth knowing about:

  • virtualenv: Third-party tool that predates venv and offers additional features
  • conda: Popular in data science, manages both Python packages and system-level dependencies
  • poetry: Modern dependency management that handles virtual environments automatically
  • pipenv: Combines pip and virtualenv into a single workflow

Each has its place, but for most projects, the built-in venv module is all you need.

Conclusion

Virtual environments are a fundamental part of Python development. They take seconds to set up and save hours of debugging dependency conflicts. If you are not using them already, start today. Your future self will thank you.

Related paths

Continue into the parts of the site most closely connected to this article.

Open Source

Public work where repeatable setup and maintainable developer workflows matter.

Explore

Projects

See the software and data work that benefits from clear tooling and dependable setup.

Explore

About

Context on the practical engineering mindset behind this kind of article.

Explore