Sub topic 12
Troubleshooting: wrong interpreter, missing activation, and broken environments
This chapter assumes you can create and activate a virtual environment with python -m venv, and that you have installed packages with python -m pip. It covers the failures you are most likely to meet and how to tell them apart. Nearly all of them come down to one question: which interpreter is actually running?
Start with the right question
A virtual environment is not a sandbox that the shell enters. It is a directory holding an interpreter (or a link to one), a pyvenv.cfg file, and a site-packages directory. Python decides at startup whether it is "inside" an environment by looking for pyvenv.cfg next to the interpreter it was started from. Activation only changes your shell's PATH so that the name python resolves to that interpreter.
So most problems reduce to one of three situations:
- The shell resolved
pythonorpipto a different interpreter than you expected. - The interpreter is correct, but the package is not installed in its
site-packages. - The environment itself is damaged or stale.
Check the first one before anything else.
Diagnose the active interpreter
Ask Python about itself rather than trusting the prompt.
python -c "import sys; print(sys.executable); print(sys.prefix); print(sys.base_prefix)"Inside an active environment, sys.executable points into .venv/bin/ (or .venv\Scripts\ on Windows). sys.prefix is the environment directory, and sys.base_prefix is the installation the environment was built from. If the two prefixes are equal, you are not running inside a virtual environment.
You can also ask the shell which executable a bare command resolves to.
which python
which pip
type -a pythonOn Windows PowerShell, use Get-Command python instead. If which pip points at /usr/bin/pip while which python points into .venv, the two commands belong to different installations. This mismatch is the reason this tutorial uses python -m pip: it runs pip with whatever interpreter python resolves to, so the package lands in that interpreter's site-packages.
python -m pip --versionThe output ends with the path of the Python that pip is attached to. Read it whenever an install behaves strangely.
Wrong interpreter
Symptom: ModuleNotFoundError right after a successful install
The install reported success, but the import fails.
ModuleNotFoundError: No module named 'requests'The usual cause is that pip install and python belong to different interpreters. Compare the two:
python -m pip --version
python -c "import sys; print(sys.executable)"
pip --versionIf pip --version shows a different path from the other two, the bare pip is not the one for your interpreter. Reinstall with python -m pip install requests.
Symptom: the script works in the terminal but not in your editor or scheduler
Editors, IDEs, cron, systemd units, and CI jobs do not run your shell's activation. Each has its own way of choosing an interpreter. Point it directly at the environment's interpreter, for example /path/to/project/.venv/bin/python. For cron and services, using the full path is more reliable than activating anything.
Symptom: `python` is not found, or is Python 2
On some Linux and macOS systems the command is python3 and python does not exist until an environment is active. Create the environment with python3 -m venv .venv. After activation, python works inside it. On Windows, use the py launcher to create it, as in py -m venv .venv.
Missing activation
Forgetting to activate is the most common cause of installing into the wrong place. The packages end up in the system or user installation, and the project environment stays empty.
Signs that you have not activated:
- The shell prompt lacks the
(.venv)prefix. This is a hint, not proof, because a customized prompt can hide it. sys.prefixequalssys.base_prefix.python -m pip listshows packages you did not install in this project, or none of the ones you did.
You can avoid depending on activation entirely by calling the environment's interpreter by path.
.venv/bin/python -m pip install requests
.venv/bin/python app.pyOn Windows, the equivalent is .venv\Scripts\python.exe -m pip install requests. This works from any shell, with or without activation, and is a good habit in scripts.
externally-managed-environment
Recent Linux distributions mark their system Python as externally managed. If you forget to activate and run python -m pip install, pip refuses:
error: externally-managed-environmentThis message is protecting the operating system's own packages. Do not override it with --break-system-packages. Activate your virtual environment and run the command again. The error is often the first sign that activation was skipped.
PowerShell refuses to run Activate.ps1
On Windows, PowerShell's execution policy may block the activation script with a message saying that running scripts is disabled on this system. Allow locally created scripts for your user only:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
.venv\Scripts\Activate.ps1Alternatively, skip activation and call .venv\Scripts\python.exe directly.
Broken environments
An environment can stop working even though its directory is still there.
The base Python disappeared or was upgraded
The environment's interpreter is typically a symlink (or a small launcher on Windows) to the base installation recorded in pyvenv.cfg. If you uninstall that Python, upgrade it to a new minor version, or remove the Homebrew or system package that provided it, the link breaks. Running .venv/bin/python then reports that the file or directory does not exist, even though you can see it in a listing.
Look at what the environment expects:
cat .venv/pyvenv.cfg
ls -l .venv/bin/pythonIf the home entry points at a directory that no longer exists, the environment cannot be repaired reliably. Rebuild it.
The environment was moved or copied
Scripts in bin/ (including pip) contain an absolute path to the interpreter in their first line, and the activation scripts record the original location. After you rename a project directory or copy an environment to another machine, those paths are wrong. Commands such as a bare pip fail with "bad interpreter", and activation sets the wrong PATH.
Do not move environments. Recreate them at the new location from your requirements file.
Packages conflict with each other
An install can succeed while leaving incompatible versions side by side, especially after you install packages one at a time. Ask pip to check:
python -m pip checkIt reports packages whose declared dependencies are missing or at an incompatible version. Fix this by installing compatible versions together, by adjusting a version specifier, or by using a constraints file to hold shared dependencies at known versions.
Mixed or corrupted state
Interrupted installs, manual edits in site-packages, and switching between very different dependency sets can leave an environment in a state that is hard to reason about. Before spending an hour debugging, rebuild it.
The rebuild procedure
Because the environment is disposable, rebuilding is the reliable fix. This assumes you have a requirements.txt made with pip freeze or written by hand.
deactivate
rm -rf .venv
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python -m pip checkOn Windows PowerShell, replace the delete step with Remove-Item -Recurse -Force .venv, and use py -m venv .venv followed by .venv\Scripts\Activate.ps1.
If you do not have a requirements file yet and the old environment still works, run python -m pip freeze > requirements.txt before you delete anything.
Other frequent install failures
| Message or symptom | Likely cause | What to do |
|---|---|---|
| `No matching distribution found for pkg==9.9` | The version does not exist, or it does not support your Python version | Check the spelling and version. Run `python -m pip index versions pkg` to list releases, or loosen the specifier. |
| `Could not find a version that satisfies the requirement` | Python version too old or too new for the package, or the index is unreachable | Check `python --version` and the package's supported versions. Check network and index settings. |
| `ResolutionImpossible` or conflicting dependencies | Two requirements demand incompatible versions of a shared dependency | Read the conflict lines, relax one specifier, or remove a constraint that is too tight. |
| Build errors mentioning a compiler or missing headers | No prebuilt wheel exists for your platform, so pip builds from source | Use a Python version the package has wheels for, or install the system build tools and libraries. |
| `Permission denied` during install | Installing into a system location without privileges | Activate a virtual environment. Do not use `sudo pip`. |
| An old version is imported after an upgrade | Another copy earlier on `sys.path`, or the process was not restarted | Print `module.__file__` and `module.__version__`, then restart the interpreter or notebook kernel. |
For the last row, a quick check shows which copy Python loaded.
import requests
print(requests.__file__)
print(requests.__version__)The path should sit under your environment's site-packages. If it points elsewhere, you have a shadowing problem or the wrong interpreter.
A file shadows the package
If you name your own script requests.py or random.py, Python imports your file instead of the library, because the script's directory comes first on sys.path. The error usually looks like AttributeError: module 'requests' has no attribute 'get'. Rename the file and delete the stale __pycache__ directory next to it.
A decision flow
Work through the checks in this order. Each step is cheap and rules out a whole class of problems.
Habits that prevent most problems
- Use
python -m pipevery time, never a barepip. - Check
sys.executablewhen a result surprises you. - Keep
requirements.txtcurrent so a rebuild takes a minute. - Treat the environment as disposable and never move it.
- Configure your editor with the interpreter path of each project's
.venv.
Exercise
You activated .venv, ran pip install httpx, and the install succeeded. Running python -c "import httpx" gives ModuleNotFoundError. Name two commands that would reveal the cause, say what output would confirm it, and give the fix.
Show answer
Run pip --version and python -c "import sys; print(sys.executable)" (or python -m pip --version). If the pip path belongs to a different installation, such as /usr/bin/pip, while the interpreter path is under .venv, the bare pip installed httpx into the wrong interpreter. The fix is to run python -m pip install httpx, which ties pip to the interpreter that will import the package.
Exercise
You renamed your project directory, and now .venv/bin/pip fails with "bad interpreter". Why did it break, and what should you do?
Show answer
Scripts in the environment's bin directory store an absolute path to the interpreter, and the environment records its original location. Renaming the directory invalidates those paths. Environments are not relocatable, so deactivate, delete .venv, create a new one in the new location, and run python -m pip install -r requirements.txt.