pip install -r requirements.txt
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.
If you wish to install a non-Debian packaged Python application,
it may be easiest to use pipx install xyz, which will manage a
virtual environment for you. Make sure you have pipx installed.
See /usr/share/doc/python3.11/README.venv for more information.
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 wish someone would explain to me what to do and how to solve it
5
Answers
This is due to your distribution adopting PEP 668 – Marking Python base environments as “externally managed”.
TL;DR: Use a venv:
Long story: Your distribution is trying to protect you against mixing
apt
provided packages andpip
provided packages. Mixing two package managers (apt
andpip
here) is always a bad idea and the source of many issues.PEP 668 is a way for distributions to explicitly tell users to avoid falling is this pit. Your distribution told you three solutions in the message, but only the 2nd one applies cleanly to your use case:
apt install python3-xxx
. It does not cleanly apply for you as you’re having arequirements.txt
, not a single dependency. It would work if you have only a few requirements in the file and can do it manually for each, likeapt install python3-xxx python3-yyy python3-zzz
. In this case there’s no weird mixing of package managers: you installedpython3
usingapt
, you’re installing your dependencies usingapt
: no surprises.python3 -m venv .venv
thensource .venv/bin/activate
, thenpip install -r requirements.txt
. In this case the installation is contained inside the.venv
directory: no mixing of what apt does and what pip does, no surprises.pipx
which does not cleanly apply to your case,pipx
is good to install and use a program, likepipx install black
, but here you need to install libraries listed in a requirement file, not a program.There’s another way, that I often use myself because I often need multiple different Python versions:
apt
installed things and does not adoptPEP 668
. I often compile many Python interpreters myself using a short bash function which use a--prefix=~/.local/
, for testing purposes. With those Pythons I use either a venv either a good oldpip install
, in this casepip
will install it in~/.local/
, again no clash betweenapt
andpip
, no bad surprises.i faced the same issue it was because of python3.11
i switched to python3.10 and it working fine now.
the message already passes the solution
can you try this
it worked for me
I was faced with this error inside
pyenv
after upgrading to Ubuntu 23.04. As part of this upgrade, the system Python was also upgraded from 3.10 to 3.11. Re-installingpyenv
didn’t help. I had to:which pip
(and alsosudo which pip
as it kept pointing me to.pyenv/shims
and root user didn’t have pyenv installed)pyenv global <version>
get-pip.py
:use
--break-system-packages
at the end of pipthis will install package in you local user directory
~/.local/lib/python3.11