Skip to content

How to Create a Python Virtual Environment (venv)

DodaTech 3 min read

In this tutorial, you'll learn about How to Create a Python Virtual Environment (venv). We cover key concepts, practical examples, and best practices.

The Problem

You install Python packages with pip and they clutter your global system. Different projects need different versions of the same package. Virtual environments solve this.

Why virtual environments are essential

When you install a package globally with pip install, it goes into your system's site-packages directory. If Project A needs Flask 2.0 and Project B needs Flask 3.0, they conflict. A virtual environment creates an isolated Python installation inside your project folder — packages in one environment do not affect any other. The venv module creates a folder with its own bin/ directory containing the Python interpreter, and a lib/ directory for packages. When activated, your shell's PATH is modified so that python, pip, and all packages resolve to the environment's copies.

Quick Fix

1. Create a virtual environment

# cd into your project directory first
python3 -m venv venv

This creates a venv folder in your project containing a standalone Python installation.

The first venv is the module name, the second is the directory name. You can name it anything — .venv is a common alternative. Use python3.11 -m venv venv to create an environment with a specific Python version.

2. Activate it

# Linux / macOS
source venv/bin/activate

# Windows (PowerShell)
venv\Scripts\Activate.ps1

# Windows (CMD)
venv\Scripts\activate.bat

Your prompt changes to (venv) $ — you're now in the virtual environment.

The activation script modifies your shell's PATH to prioritize the virtual environment's directories. The (venv) prefix in your prompt is a visual reminder. If you do not see it, check that the venv folder exists in your current directory.

3. Install packages

pip install requests flask

Packages are installed inside venv/, not globally.

You can now use pip install normally. Every package is stored in venv/lib/python3.x/site-packages/. If you run pip list, you will see only the packages installed in this environment, not your global ones. This keeps each project's dependencies separate and manageable.

4. Save your dependencies

pip freeze > requirements.txt

The pip freeze command outputs a list of installed packages with their exact version numbers. The > redirect writes this list to a file. Commit requirements.txt to your repository so that other developers (or your production server) can recreate the exact same environment with pip install -r requirements.txt. This ensures reproducible builds across different machines.

5. Deactivate when done

deactivate

The deactivate function restores your original PATH. If you close the terminal without deactivating, the environment deactivates automatically — but it is good practice to call deactivate explicitly when you finish working on a project.

Restoring on Another Machine

python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

This is the standard way to set up a Python project on a new machine: create the environment, activate it, install dependencies. This same process is used in CI/CD pipelines and Dockerfiles.

One-Liner Workflow

python3 -m venv venv && source venv/bin/activate && pip install -r requirements.txt

The && operator chains the three commands so that each one runs only if the previous one succeeded. If venv creation fails (for example, if python3-venv is not installed), the activation and install steps are skipped. This is useful for setup scripts, Dockerfiles, or anytime you want to set up an environment in a single command.

Prevention

Do not commit the venv folder. Add venv/ or .venv/ to your .gitignore. The environment is machine-specific and can be recreated from requirements.txt.

Always use virtual environments for every project. Build the habit of creating and activating an environment as the first step when starting a new project.

Use pip-tools or poetry for complex projects. For projects with many dependencies, pip freeze outputs a flat list that can be hard to manage. These tools provide dependency resolution and locking.

Keep requirements.txt up to date. Run pip freeze > requirements.txt whenever you add or remove a package.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro