Sub topic 12
Troubleshooting: wrong interpreter, missing activation, and broken environments
This chapter assumes you can create and activate a venv, and that you know python -m pip install. Most environment problems come from one cause: the python or pip you ran is not the one you meant. The fixes below start from that question.
First check: which interpreter is running?
Before changing anything, ask Python where it lives.
python -c "import sys; print(sys.executable); print(sys.prefix); print(sys.base_prefix)"
python -m pip --versionInside an active environment, sys.prefix and sys.base_prefix differ, and sys.executable points into your .venv folder. If the two prefixes are equal, you are using the base installation. On Linux and macOS, which python shows the same information. On Windows PowerShell, use Get-Command python.
Problem 1: installing into the wrong interpreter
Symptom: pip install succeeds, yet import fails. A bare pip command may belong to a different Python than the python command you run. Using python -m pip removes that mismatch, because pip runs under the interpreter you named.
python -m pip install requests
python -c "import requests; print(requests.__file__)"The printed path should contain .venv. If it points to a system folder, you installed into the base installation. Activate the environment, then install again.
Problem 2: forgetting to activate
A new terminal does not remember activation. Each shell session starts with the base interpreter. You can skip activation completely by calling the environment's interpreter by path.
.venv/bin/python -m pip install requests
.venv/bin/python app.pyOn Windows, the path is .venv\Scripts\python.exe. This form is also the safest choice in scripts, cron jobs, and editors, where no activation step runs.
Problem 3: a broken environment
An environment breaks when its base interpreter changes or moves. Typical causes are a Python upgrade, a removed Python version, or a renamed project folder. Symptoms include "bad interpreter" errors, a missing python file, or pyvenv.cfg pointing to a path that no longer exists.
Moving or renaming a venv is unsupported, because activation scripts and installed command-line tools record absolute paths. Do not repair these environments. Delete and rebuild them, which works because the requirements file holds the real state.
deactivate
rm -rf .venv
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txtCheck the path before running rm -rf, since it deletes without confirmation.
Problem 4: conflicts and missing dependencies
If a package fails at runtime with an unexpected import error, run:
python -m pip checkIt reports installed packages whose dependencies are missing or incompatible. Resolve a conflict by adjusting a version specifier, then reinstall.
Quick reference
| Symptom | Likely cause | Fix |
|---|---|---|
| `ModuleNotFoundError` after a successful install | Installed into a different interpreter | Use `python -m pip`; compare `sys.executable` |
| Prompt has no `(.venv)` prefix | Environment not activated in this shell | Run the activate script |
| `bad interpreter` or missing `python` | Base Python moved or removed | Delete and rebuild the environment |
| `pip check` reports conflicts | Incompatible version requirements | Adjust specifiers and reinstall |
| Command works in terminal, fails in editor | Editor uses another interpreter | Select the `.venv` interpreter in the editor |
Exercise
You activated .venv, ran pip install requests, and import requests still fails. Name two commands that help find the cause, and say what each shows.
Show answer
Run python -c "import sys; print(sys.executable)" to see which interpreter runs your code, and python -m pip --version to see which interpreter pip belongs to. If they point to different locations, or neither is inside .venv, the bare pip belongs to another Python. Reinstall with python -m pip install requests.