Python virtual environments: how to set one up and fix the errors

Test setup: Ubuntu 24.04 with Python 3.12 and pip 24.0, November 29, 2026. Each fix linked below says whether we reproduced that error.

A virtual environment is a folder with its own Python and its own packages, separate from the Python your operating system uses. Most pip errors go away once you install into one, because pip then writes to a folder you own instead of a system location.

Create and use one

python3 -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
python -m pip install requests

We ran these on Ubuntu 24.04’s Python 3.12: the system pip refused to install with error: externally-managed-environment, and the same install inside .venv worked. While the environment is active, python and pip point at .venv, and your prompt usually shows (.venv).

Activate it on your system

Activation only changes your current terminal session; open a new terminal and you need to activate again. The script depends on your shell:

Shell Command
bash or zsh (Linux, macOS) source .venv/bin/activate
fish source .venv/bin/activate.fish
csh or tcsh source .venv/bin/activate.csh
Windows Command Prompt .venv\Scripts\activate.bat
Windows PowerShell .venv\Scripts\Activate.ps1

Our test environment had activate, activate.fish, activate.csh and Activate.ps1 in .venv/bin. If PowerShell refuses to run the script, its execution policy is blocking it; Set-ExecutionPolicy -Scope CurrentUser RemoteSigned is the setting Python’s venv documentation points to. We didn’t test on Windows.

You can also skip activation and call the environment’s Python directly, which is handy in scripts and cron jobs: .venv/bin/python app.py.

Save and reinstall your packages

python -m pip freeze > requirements.txt
python -m pip install -r requirements.txt

In our test, a second environment built from that file imported requests 2.34.2, the same version as the first.

Leave it, delete it or move it

  • deactivate returns your shell to the system Python.
  • To delete an environment, delete its folder: rm -rf .venv.
  • Don’t move or rename an environment. .venv/pyvenv.cfg and the scripts inside it store absolute paths, so a moved environment points at the old location. Delete it and create a new one from requirements.txt instead.

venv, virtualenv, conda or uv?

Tool Use it when
venv The default. Built into Python 3, nothing to install
virtualenv You need environments for an older Python, or its extra options
conda Your project needs non-Python libraries (CUDA, GDAL) managed for you
uv You want one tool that creates environments and installs packages; uv venv makes a standard .venv

All four give you an isolated folder; the commands on this page work with venv and virtualenv environments. We tested venv only.

If python -m venv fails on Debian or Ubuntu

Debian and Ubuntu ship the venv module separately. Without it, creating an environment stops with a message that ensurepip is not available. Install the package for your Python version, then try again:

sudo apt install python3-venv       # or python3.12-venv to match your version

Our test system already had it installed, so we didn’t see this message ourselves.

Diagnose before you fix

Most Python setup errors come from running a different Python than the one you installed into. Check which one runs:

python3 -c "import sys; print(sys.executable)"
python3 -m pip --version

Both lines should name the same folder. Always install with python -m pip rather than plain pip, so the package goes to the interpreter you just checked.

Habits that prevent most pip errors

  • One virtual environment per project, created in the project folder.
  • Install with python -m pip install, not sudo pip.
  • For command-line tools you want everywhere (black, httpie), use pipx, which gives each tool its own environment.
  • Point your editor at .venv/bin/python so it imports what you installed.

Every Python error we cover

Environment errors

Install and PATH errors

FAQ

Should I always use a virtual environment?

For any project you install packages for, yes. It keeps each project's versions apart and avoids the externally-managed-environment error on Linux and Homebrew Python. Quick one-off scripts that only use the standard library don't need one.

Should I commit the .venv folder?

No. Add .venv/ to .gitignore and commit a requirements.txt (from python -m pip freeze) or your pyproject.toml instead.