Quick answer
pip and venv errors usually happen when pip installs packages into the wrong interpreter, when the virtual environment is not activated, or when system and user packages get mixed. The fix is to create a new venv environment, activate it, and install packages inside it, while checking that the pip and python commands point to the same place.
How to recognize the problem
- pip install reports Permission denied or externally-managed-environment
- Module not found even though the package is installed
- python and python3 commands show different versions
- venv does not activate or activation is lost after closing the terminal
- pip list shows packages you did not install in that environment
Most common causes
Wrong interpreter
The python command points to one interpreter and pip to another. Packages get installed where the application cannot see them.
venv not activated
The terminal is still in the system environment, so pip installs globally instead of into the virtual environment.
System packages and PEP 668
Newer Linux distributions block global pip installs under PEP 668; use venv or `pipx`, not `--break-system-packages`.
Mixing user and system installs
Packages installed with --user or through the system package manager can mask those in the venv environment.
Corrupted or partially installed package
An interrupted installation or a cached broken wheel leads to errors on import or update.
Step-by-step fix
- 1
Check which interpreter and pip you are using
In the terminal run
python --version,python3 --version,pip --versionandwhich python(on Windowswhere python). Compare the paths: if pip points to a different directory than the interpreter, that is the cause of most errors. If needed, usepython -m pip --versionto see the pip tied to a specific interpreter. - 2
Create a new virtual environment
From the project root directory run
python -m venv .venv(on Linux and macOS usepython3 -m venv .venvif needed). On Windows usepy -m venv .venvif the Python launcher is installed. The name.venvis a convention and makes it easy to recognize; avoid spaces in the project path. - 3
Activate venv and check the paths
On Windows (PowerShell) run
.venv\Scripts\Activate.ps1, in Command Prompt.venv\Scripts\activate.bat, and on macOS and Linuxsource .venv/bin/activate. After activation the prompt should show(.venv). Check withwhich pythonandwhich pip(on Windowswhere pythonandwhere pip) that both are inside.venv. - 4
Update pip inside the environment
While venv is active, run
python -m pip install --upgrade pip setuptools wheel. This avoids errors with outdated pip that appear when installing modern packages. Do not usesudo pipor global pip. - 5
Install packages and record dependencies
Install with
python -m pip install package-name. If you have arequirements.txt, usepython -m pip install -r requirements.txt. After a successful install, save the state withpython -m pip freeze > requirements.txtso the environment is reproducible; ifrequirements.txtalready exists, make a backup first because the file is overwritten. - 6
Clear the cache and reinstall the problematic package
If the installation fails, run
python -m pip cache purgeand repeat the installation. For stubborn cases usepython -m pip install --force-reinstall package-name. Check whether the package has a wheel for your platform and Python version. - 7
Fix the externally-managed-environment error
On newer Linux distributions global pip is blocked. The solution is to work inside a venv environment, not to use
--break-system-packages. If you need a system tool, install it through the distribution package manager (e.g.apt), not through pip. - 8
Make sure the IDE uses the same environment
In VS Code open the Command Palette (
Ctrl+Shift+P, on macOSCmd+Shift+P) and choosePython: Select Interpreter, then select.venv. In PyCharm checkSettings > Project: <project name> > Python Interpreter. The IDE can use a different interpreter than the terminal and causeModuleNotFoundError.
If nothing helps: isolation and diagnostics
Create a completely new directory, a new venv environment, and install only the package that is causing the problem. If it works there, the cause is a conflict with another package or an outdated requirements.txt. Use python -m pip check to find incompatible dependencies and python -m pip list --outdated to see what is outdated. To reproduce the error, run Python with -v or -X importtime to see where the module is loaded from. If you use multiple Python versions, consider pyenv (macOS/Linux) or the py launcher (Windows) to explicitly choose the version. Do not install packages globally to "bridge" the problem because the fault returns with the next project.
How to prevent it
- Always use venv per project and never install packages globally.
- Record dependencies in requirements.txt and pin versions.
- Check that the IDE and terminal use the same interpreter.
- Avoid sudo pip and --break-system-packages.
When to call a professional
If the errors come from system Python installations, conflicting versions, or a business environment with restricted permissions, contact IT support to safely isolate the environment and preserve existing tools.
Software and product development · Custom apps, SaaS and an MVP in 4 to 8 weeksFAQ
Why does pip say the package is already installed but Python cannot see it?
Most likely you installed the package into another interpreter or into the global environment. Check the paths with `which python` and `which pip` (on Windows `where python` and `where pip`) and make sure they are inside the same venv environment.
Can I use venv and global packages together?
You can, but it is not recommended because it makes reproduction harder. If you need a tool available everywhere, install it through the system package manager or use `pipx` for isolated CLI tools.
What does the externally-managed-environment error mean?
It is a protection introduced by new Linux distributions to prevent pip from changing system packages. The solution is to work inside a venv environment, not to bypass it with the `--break-system-packages` flag.