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

2

7

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!

Twopiece answered 24/1 at 2:56 Comment(2)
type -a python — is it the Python from the env/bin ?Photolysis
hm, I get the following: python is aliased to `/usr/bin/python3.11' python is /~/env/bin/python python is /usr/bin/python python is /bin/python I should have mentioned that I have python3 aliased to python on my computerTwopiece
P
4

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.

Photolysis answered 26/1 at 8:46 Comment(7)
Thank you, this fixed the problem. However, I'm not sure where I should put unalias python python3 inside of the activate script. Would it be fine to just put it at the very top?Twopiece
@Twopiece Doesn't matter. At any place. The very top is ok.Photolysis
Why don't you use pipx? It manages virtual environments for you.Nonchalance
where is this activate script?Assurgent
@Assurgent How do you activate virtual environments?Photolysis
pipx does not work on MACOS @NonchalanceEmeraldemerge
@Emeraldemerge "pipx does not work on MACOS" Hard to believe. At pypi.org/project/pipx it marked "OS Independent" and there're installation instructions for macOS using brew.Photolysis
L
1

Experienced the same issue on ubuntu 24. I solved it by permanently deleting the old venv folder and then making a new one by following steps 1-3. Also, make sure you are using the right virtual environment by running where python.

Lachesis answered 19/8 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.