pythonpython-3.xraspberry-pi

Running pip install in virtual environment tries to install packages in default python directory


I am trying to install modules for python on my raspberry pi 5 in a virtual environment but it just says that the environment is externally managed.

I started with activating the virtual environment and trying to install the package I needed but it just told me the environment was externally managed.

clock@system-time:/clock $ source .venv/bin/activate
(.venv) clock@system-time:/clock $ sudo python3 -m pip install inputimeout
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    For more information visit http://rptl.io/venv

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

I checked which pip and python it was using and it was the one from the virtual environment.

(.venv) clock@system-time:/clock $ which pip
/clock/.venv/bin/pip
(.venv) clock@system-time:/clock $ which python
/clock/.venv/bin/python

So I tried using --break-system-packages because I though maybe there was just a something wrong. It downloaded and installed the package.

(.venv) clock@system-time:/clock $ sudo python3 -m pip install inputimeout --break-system-packages
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting inputimeout
  Downloading inputimeout-1.0.4-py3-none-any.whl (4.6 kB)
Installing collected packages: inputimeout
Successfully installed inputimeout-1.0.4
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

I then tried checking my list of installed packages and it was not there.

(.venv) clock@system-time:/clock $ pip list
Package    Version
---------- -------
pip        23.0.1
setuptools 66.1.1

So I exited and checked the list for the default installation of python and it was there.

(.venv) clock@system-time:/clock $ deactivate
clock@system-time:/clock $ pip list
Package                            Version
---------------------------------- ----------
arandr                             0.1.11
asgiref                            3.6.0
astroid                            2.14.2
asttokens                          2.2.1
av                                 10.0.0
Babel                              2.10.3
beautifulsoup4                     4.11.2
blinker                            1.5
certifi                            2022.9.24
chardet                            5.1.0
charset-normalizer                 3.0.1
click                              8.1.3
colorama                           0.4.6
colorzero                          2.0
cryptography                       38.0.4
cupshelpers                        1.0
dbus-python                        1.3.2
dill                               0.3.6
distro                             1.8.0
docutils                           0.19
executing                          2.0.1
Flask                              2.2.2
gpiozero                           2.0
html5lib                           1.1
icecream                           2.1.3
idna                               3.3
importlib-metadata                 4.12.0
inputimeout                        1.0.4
...

I also tried following this but it just didn't work either.


Solution

  • sudo runs a new root shell which has no idea about your current shell's settings (including which virtual environment is active).

    Absolutely don't use sudo if you want to install things into the currently active user-owned virtual environment.

    Anyway, the entire purpose of having a virtual environment is that it's completely controlled by yourself; so you do not need root privileges to modify it (and if you somehow managed to, creating root-owned files in there would wreck it, because then you can no longer change those files without becoming root again).