Skip to content

Python pip and venv errors: how to set up a correct environment

Medium 20 min Updated:

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. 1

    Check which interpreter and pip you are using

    In the terminal run python --version, python3 --version, pip --version and which python (on Windows where python). Compare the paths: if pip points to a different directory than the interpreter, that is the cause of most errors. If needed, use python -m pip --version to see the pip tied to a specific interpreter.

  2. 2

    Create a new virtual environment

    From the project root directory run python -m venv .venv (on Linux and macOS use python3 -m venv .venv if needed). On Windows use py -m venv .venv if the Python launcher is installed. The name .venv is a convention and makes it easy to recognize; avoid spaces in the project path.

  3. 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 Linux source .venv/bin/activate. After activation the prompt should show (.venv). Check with which python and which pip (on Windows where python and where pip) that both are inside .venv.

  4. 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 use sudo pip or global pip.

  5. 5

    Install packages and record dependencies

    Install with python -m pip install package-name. If you have a requirements.txt, use python -m pip install -r requirements.txt. After a successful install, save the state with python -m pip freeze > requirements.txt so the environment is reproducible; if requirements.txt already exists, make a backup first because the file is overwritten.

  6. 6

    Clear the cache and reinstall the problematic package

    If the installation fails, run python -m pip cache purge and repeat the installation. For stubborn cases use python -m pip install --force-reinstall package-name. Check whether the package has a wheel for your platform and Python version.

  7. 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. 8

    Make sure the IDE uses the same environment

    In VS Code open the Command Palette (Ctrl+Shift+P, on macOS Cmd+Shift+P) and choose Python: Select Interpreter, then select .venv. In PyCharm check Settings > Project: <project name> > Python Interpreter. The IDE can use a different interpreter than the terminal and cause ModuleNotFoundError.

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 weeks

FAQ

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.

Share
Didn't find a fix?The AI IT assistant is available 24/7. Describe the problem or send a screenshot of the error and get instructions for your case.Ask the AI IT assistant