Python guide · Last updated

Python Virtual Environments: Create and Activate a venv

To create a Python virtual environment, run python -m venv .venv in your project folder. Activate it with source .venv/bin/activate on macOS and Linux or .venv\Scripts\activate on Windows, and leave it with deactivate. A virtual environment is a self-contained folder with its own Python interpreter and its own installed packages, so each project can pin its own dependency versions without breaking other projects or the system Python. This guide covers creating and activating a venv on every platform (including the PowerShell execution-policy error), managing requirements.txt, selecting the interpreter in VS Code, the alternatives virtualenv, conda and uv, and fixes for the most common errors.

Quick answer

Run these from your project's root folder. .venv is the conventional name: editors detect it automatically and most .gitignore templates already exclude it.

bash
# macOS / Linux
python3 -m venv .venv
source .venv/bin/activate
pip install requests
deactivate
powershell
# Windows (PowerShell)
py -m venv .venv
.venv\Scripts\Activate.ps1
pip install requests
deactivate

Why use a virtual environment?

Without one, pip install puts packages into a single global location shared by every project. Sooner or later two projects need different versions of the same library, an upgrade for one breaks the other, and nobody remembers which packages a project actually needs. On recent Linux distributions and Homebrew Python, installing into the system interpreter is now blocked altogether with an externally-managed-environment error (PEP 668), precisely to stop pip from breaking OS tools.

A virtual environment solves this by giving each project its own site-packages folder and its own copies of python and pip. It is cheap (a few megabytes before you install anything), disposable and easy to recreate from a requirements file.

Create a virtual environment with python -m venv

The venv module has shipped with Python since 3.3, so there is nothing to install. On macOS and Linux the command is usually python3; on Windows the py launcher is the most reliable way to call Python. The environment uses whichever Python version created it, so to target a specific version, call that interpreter explicitly.

Useful options: --prompt changes the name shown in your shell prompt, --upgrade-deps upgrades pip in the new environment right away, and --system-site-packages lets the environment see globally installed packages (rarely what you want). On Debian and Ubuntu, venv is split into a separate package; if you see "ensurepip is not available", install python3-venv first.

bash
python3 -m venv .venv
python3.12 -m venv .venv                 # a specific version (macOS / Linux)
py -3.12 -m venv .venv                   # a specific version (Windows)
python3 -m venv .venv --prompt myapp --upgrade-deps

sudo apt install python3-venv            # Debian / Ubuntu, if venv is missing

Activate the virtual environment

Activation adjusts your current shell so that python and pip point to the environment's copies, and adds its name to your prompt, such as (.venv). It only affects the terminal you run it in. The script differs per shell and operating system: on Windows the environment has a Scripts folder, on macOS and Linux a bin folder.

Tip

Activation is a convenience, not a requirement. Running the environment's interpreter directly, such as .venv/bin/python app.py or .venv\Scripts\python app.py, always uses the right packages. That is the most reliable form in cron jobs, Dockerfiles and CI scripts.

macOS and Linux

bash
source .venv/bin/activate          # bash / zsh
source .venv/bin/activate.fish     # fish
source .venv/bin/activate.csh      # csh / tcsh

Windows Command Prompt

bat
.venv\Scripts\activate.bat

Windows PowerShell and execution policy

In PowerShell, the activation script is Activate.ps1. On a fresh Windows install it often fails with "running scripts is disabled on this system", because the default execution policy blocks local scripts. Allow locally created scripts for your user account once, or bypass the policy only for the current session if you cannot change settings.

powershell
.venv\Scripts\Activate.ps1

# If blocked: allow local scripts for your user (one-time)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# Or only for this PowerShell window
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

Git Bash on Windows

bash
source .venv/Scripts/activate

Check it worked, and deactivate

After activating, confirm which interpreter your shell is using; the path should be inside .venv. sys.prefix differs from sys.base_prefix only inside a virtual environment. When you are done, run deactivate to return to the global Python, or simply close the terminal. To delete an environment, deactivate it and remove the .venv folder; nothing else on the system refers to it.

bash
which python            # macOS / Linux  -> /path/to/project/.venv/bin/python
where python            # Windows        -> C:\path\to\project\.venv\Scripts\python.exe
python -c "import sys; print(sys.prefix != sys.base_prefix)"   # True inside a venv

deactivate

Manage dependencies with requirements.txt

With the environment active, pip install puts packages inside it. Record exact versions with pip freeze > requirements.txt and commit that file, never the .venv folder itself. Virtual environments contain absolute paths and platform-specific binaries, so they are not portable between machines or even between folders: if you move or rename the project, delete .venv and recreate it. Anyone cloning the repository rebuilds an identical environment from the requirements file.

pip freeze lists every installed package, including dependencies of dependencies. Some teams keep a short hand-written list of direct dependencies and generate the pinned file with a tool such as pip-tools or uv.

bash
pip install django==5.2 requests
pip freeze > requirements.txt
pip install -r requirements.txt      # on another machine, inside a new venv
pip list --outdated

echo ".venv/" >> .gitignore

Select the venv in VS Code

With the Python extension installed, VS Code usually detects a .venv folder in the workspace and offers to use it. If not, open the Command Palette (Ctrl+Shift+P, or Cmd+Shift+P on macOS), run "Python: Select Interpreter" and pick the entry that points to .venv, or choose "Enter interpreter path" and browse to .venv/bin/python (.venv\Scripts\python.exe on Windows). New integrated terminals then activate the environment automatically, and the run button, debugger, linting and Jupyter notebooks all use it. PyCharm offers the same choice under Settings, Project, Python Interpreter.

virtualenv, conda and uv

venv is enough for most projects, but three alternatives are common and solve slightly different problems.

virtualenv

The third-party package that inspired venv. It creates environments faster, can target Python versions other than the one running it, and supports old interpreters. The environments look and activate exactly like venv ones.

bash
pip install --user virtualenv
virtualenv .venv
virtualenv -p python3.11 .venv

conda

Conda (via Miniconda, Anaconda or Miniforge) manages environments that can include non-Python dependencies such as CUDA, MKL or GDAL, and it installs Python itself. It is popular for data science and machine learning. Environments live in a central folder and are activated by name. Prefer conda install for packages available in conda channels and use pip inside the environment only for the rest.

bash
conda create -n myenv python=3.12
conda activate myenv
conda deactivate
conda env export > environment.yml
conda env create -f environment.yml
conda env list

uv

uv is a very fast, Rust-based replacement for pip, venv and more. uv venv creates a standard .venv, downloading the requested Python version if it is not installed, and uv pip installs packages into it far faster than pip. For new projects, uv init, uv add and uv run manage the environment and a lockfile for you, so you rarely need to activate anything.

bash
uv venv                          # creates .venv
uv venv --python 3.12
uv pip install -r requirements.txt

uv init myapp && cd myapp
uv add requests                  # updates pyproject.toml and uv.lock
uv run python main.py            # runs inside the project environment

Common errors and fixes

Most virtual environment problems come down to using a different interpreter than the one you think you are using.

  • ModuleNotFoundError after installing a package: the package went into one interpreter and your code runs with another. Activate the venv, or select it in your editor, and run python -m pip install ... so pip and python always match.
  • error: externally-managed-environment: you ran pip against the system Python. Create and activate a venv first; do not reach for --break-system-packages.
  • "running scripts is disabled on this system": PowerShell's execution policy. Run Set-ExecutionPolicy RemoteSigned -Scope CurrentUser.
  • 'python' is not recognized on Windows, or the Microsoft Store opens: use the py launcher, or disable the python.exe App execution aliases in Settings and reinstall Python with "Add to PATH" checked.
  • "The virtual environment was not created successfully because ensurepip is not available": install python3-venv (Debian/Ubuntu) and try again.
  • The venv broke after moving or renaming the folder, or after upgrading Python: delete .venv and recreate it from requirements.txt.

Python venv: FAQ

Quick answers to common questions

How do I create a virtual environment in Python?

Run python -m venv .venv in your project folder (use python3 on macOS and Linux, or py on Windows). This creates a .venv folder containing an isolated Python interpreter and pip.

How do I activate a venv on Windows?

In Command Prompt run .venv\Scripts\activate.bat. In PowerShell run .venv\Scripts\Activate.ps1; if scripts are blocked, run Set-ExecutionPolicy RemoteSigned -Scope CurrentUser once first.

What is the difference between venv and virtualenv?

venv is built into Python 3. virtualenv is a third-party package that is faster, supports older Python versions and can create environments for other interpreters. Both produce environments that work the same way.

Should I commit the .venv folder to Git?

No. Add .venv/ to .gitignore and commit requirements.txt (or pyproject.toml and a lockfile) instead. Virtual environments contain absolute paths and platform-specific files.

Should I use venv, conda or uv?

Use venv for standard Python projects, uv if you want the same result much faster with lockfiles and Python version management, and conda when you need non-Python libraries such as CUDA or GDAL.

Related tools and references

More developer guides