python-3.xinstallationpippackagepython-venv

Getting 'externally-managed-environment' error when trying to install with pip, DESPITE being in a virtual environment


First thing's first: my OS is openSUSE Tumbleweed, my Python version is 3.11.7, and my pip version is 23.2.2

My goal is as follows: I want to use pip to install the pytictoc package into a virtual environment I create with Python.

My understanding of how to do this is:

  1. Run python3 -m venv env to create a directory called env for my virtual environment.
  2. Then, within that directory, I run source bin/activate to activate the virtual environment
  3. Ensure that the virtual environment is indeed running; (env) should appear at the start of the current line, and any subsequent lines.
  4. Run python -m pip install pytictoc to install the pytictoc package to the virtual environment

When trying to execute these steps, 1-3 go without any problems, however running the command in step 4 yields the externally-managed-environment error. Here is the full error message:

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try
    zypper install python311-xyz, where xyz is the package
    you are trying to install.
    
    If you wish to install a non-rpm packaged Python package,
    create a virtual environment using python3.11 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip.
    
    If you wish to install a non-rpm packaged Python application,
    it may be easiest to use `pipx install xyz`, which will manage a
    virtual environment for you. Install pipx via `zypper install python311-pipx` .

As far as I can tell, I'm doing everything right, so I'm lost as to how to approach this. I've seen solutions involving tacking on --break-system-packages to pip, but this seems to be for cases where the user is NOT using a virtual environment. As far as I can tell, there are no other forums where someone has my exact problem, except this askubuntu question. The user responded to the top answer, explaining that they still receive the error message despite being in a virtual environment. However, this comment didn't receive any response.

There may be other solutions, such as using pipx, or other virtual environments like conda. Unfortunately, for me it is essential that I make the virtual environment with venv, and that I use pip to install the package "pytictoc" specifically. I cannot install the package system-wide.

I appreciate any help or response anyone is able to offer. Thank you for taking the time to read this!


Solution

  • You have python aliased (in your shell) to /usr/bin/python3.11. So when you run python -m pip in command line you actually run global pip, not the pip from the venv, and the global pip complains about "externally managed environment".

    When you activate a virtual environment you should unalias python; you can insert unalias python python3 into activate script. Or always run venv/bin/python -m pip with absolute or relative path.