732

When I run pip install xyz on a Linux machine (using Debian or Ubuntu or a derived distro), I get this error:

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.

What does this error mean? How do I avoid it? Why doesn't pip install xyz work like it did before I upgraded my system using sudo apt upgrade?

3

37 Answers 37

479

The proper way to install Python libraries and applications is to install them in a Python virtual environment whenever possible (the exceptions to this rule are quite rare).

The error message describes two common ways to accomplish this: either by creating a virtual environment yourself, or for applications, by using pipx - a tool which will create a virtual environment for you and install the application in that virtual environment.

pipx is strongly recommended for installing applications - i.e., when you will primarily use the installed code from the command line. On Debian systems and Debian-based systems such as Ubuntu, you can install pipx using apt, and then use pipx to install the application:

apt install pipx
pipx install some-python-application

For libraries - i.e., when you will use the code primarily by importing it in your own projects - typically you should create a virtual environment yourself. You can do this with venv from the standard library:

python -m venv my-venv
my-venv/bin/pip install some-python-library

See also this answer on a duplicate question for more details.

(Commonly, your own project may need several libraries. Make one virtual environment and install the libraries that your project needs side by side in that virtual environment.)


If you have considered your options carefully and are still sure that you want to install packages "system-wide" and risk breaking your system (for example, by overwriting libraries that were part of tools written in Python that came with your system - such as apt), Pip needs to be given permission to do so.

There are a few ways to do this:

  • For a single use of pip, add the --break-system-packages argument to the command.

  • Add these lines to ~/.config/pip/pip.conf (this will enable every future run of Pip to break system packages:

    [global]
    break-system-packages = true
    
  • Use Pip's config command to edit the above file (credit to The Matt from the comments):

    python3 -m pip config set global.break-system-packages true
    

Theoretically, removing or renaming the "marker" file (/usr/lib/python3.x/EXTERNALLY-MANAGED) would also disable the block, but this is a bad idea - the file was put there for a reason, and it's at least as easy to use the intended mechanisms instead.

12
  • 3
    1. As it suggests, some packages can be installed via apt install xyz, but that didn't work for some packages for me 2. How can I use Jupyter Notebooks with venv?
    – sagitta
    Commented May 22, 2023 at 10:58
  • 1
    @sagitta You can create a venv and then select it as a notebook kernel. If you are using VSCode you can press F1 and then Notebook: Select Notebook Kernel. If you run jupyter from console, just activate the venv, install jupyter and start it.
    – maciek97x
    Commented May 22, 2023 at 12:16
  • 1
    Problem is in debian 12, it has not python2-virtualenv. And pip return error on install virtualenv for python2. So how can be python2 virtualenv installed without using break-system-packages ?
    – Some
    Commented Sep 5, 2023 at 14:26
  • 4
    @Some I believe it will only break system packages if there is a system package to break. Since you say your OS doesn't have the package, you should be fine to use --break-system-packages just once, to install python2-virtualenv. (If debian ever does add it as a package, you may want to pip uninstall it.) Commented Oct 8, 2023 at 6:12
  • 2
    @flyingsheep use-case: installing Tensorflow user-local, but because I have more than one project that uses it I can't afford the multi-GB disk space cost multiple times, so a venv is impractical. Commented Oct 17, 2023 at 12:07
326

I've got this error since Python 3.11+.

Consider relevant comments received in this post from Alok and JackLeEmmerdeur :
This deletion of file is not safe. This can lead to Broken Package Management, Conflicting Installations and Permission Issues

So, find below the updated answer that allowed me to resolve this issue without the risk of compromising the system :

sudo mv /usr/lib/python3.11/EXTERNALLY-MANAGED /usr/lib/python3.11/EXTERNALLY-MANAGED.old
11
  • 10
    Just beware that this may still break your system Commented Oct 24, 2023 at 1:04
  • 6
    rm vs mv ..just in case you would like to revert back.
    – P....
    Commented Nov 21, 2023 at 23:04
  • 1
    This file does not exist on mac -- what is the mac/brew equivalent? Commented Apr 1 at 1:12
  • 4
    @JackLeEmmerdeur Even better: sudo mv /usr/lib/python3.11/EXTERNALLY-MANAGED /etc/motd :)
    – x-yuri
    Commented Apr 4 at 2:00
  • 9
    find the offender — linux: find /usr/lib -name EXTERNALLY-MANAGED; brewed python on macos with apple chips: find /opt/homebrew -name EXTERNALLY-MANAGED intel mac: find /usr/local -name EXTERNALLY-MANAGED — once you identify the exact path delete the file with rm or by appending -delete to the original find command
    – ccpizza
    Commented May 22 at 16:03
155

The --break-system-packages flag in pip allows to override the externally-managed-environment error and install Python packages system-wide.

pip install package_name --break-system-packages

Note: Usage of this flag shouldn't be abused.

4
  • 9
    True, but usage of this flag shouldn't be abused. Large projects like Jupyterlab ship with many dependencies (which keep increasing with each installed extension), so by choosing this route instead of installing through pipx (or from the official repos if it's available), one is bound to have a conflict with a system-maintained package.
    – micmalti
    Commented Aug 20, 2023 at 18:57
  • 1
    Could this approach have any potentially negative consequences if used only to install a single package while building a docker image? Commented Feb 5 at 11:24
  • @user1053510 If you're like me and using a php base image I'd argue it's ok to run it like this if your container is going to run on private subnets and be used for queued work. I may figure out better user permissions as I lean heavier into docker for my worker servers and add a new answer geared towards running python as a sister service on the container. Commented May 22 at 2:23
  • Sometimes you just want something that used to work to work again. This is the easiest quick-fix. It's unclear who to blame for the breaking change. Was it Python 3.11? Was it Ubuntu/Debian? Who knows... Commented Dec 5 at 18:15
79

Currently, some of the most upvoted answers are teaching you ways to ignore this problem. This is like telling you to take pain-killers to stop feeling the pain of broken glass in your throat, rather than telling you to stop eating broken glass. Stop eating broken glass. There are much better ways to install packages from PyPI than using the --break-system-packages flag, or worse, deleting the EXTERNALLY-MANAGED file.

The error is telling you that the environment is externally managed. Your Debian distribution already handles installation of Python libraries using APT. For example, if you wanted to install the requests Python library, you can run:

sudo apt install python3-requests

These files get installed in /usr/lib/python3/dist-packages/, as you can see from the output of the dpkg -L command:

$ dpkg -L python3-requests 
/usr/lib/python3/dist-packages/requests
/usr/lib/python3/dist-packages/requests/__init__.py
/usr/lib/python3/dist-packages/requests/__version__.py
/usr/lib/python3/dist-packages/requests/_internal_utils.py
# ...

If you run pip install requests, where should the files be installed? Should they be installed in /usr/lib/python3, or ~/.local/lib/python3/site-packages/ or somewhere else? The version installed from PyPI using pip might not be the same version included in the Debian package. What if the overwrite doesn't succeed? What if you have two requests packages installed system-wide? Should pip learn how to uninstall APT packages? You probably have hundreds of Debian packages that depend on Python, what if one of them breaks because of these conflicting versions of requests? Can you easily undo the pip installation? Will you even realise that the weird errors you are experiencing when you launch gedit or something are because of this? This seems like a recipe for disaster. You used to be able to use pip to install Python packages system-wide, and it caused so many problems, that it now throws this error message instead.

So, what can you do instead?

1. Install packages using APT

You can install Python packages system-wide using APT. For example, you can install requests like this:

sudo apt install python3-requests

This version might not be the latest version found in PyPI. And not all packages on PyPI have been packaged for the Debian repositories. But fear not, there are other solutions.

Or: 2. Use pip in a virtual environment

If you haven't yet learned a tool to set up a virtual environment, I highly recommend it. All Python programmers should learn one. I recommend venv or virtualenv to beginners. To install venv, run:

sudo apt install python3-venv

Then create a virtual environment in your project directory like this:

 python3 -m venv .venv

Now activate your virtual environment by running:

 source .venv/bin/activate

This modifies your PATH environment variable to include .venv/bin/. Now you can install PyPI packages using pip into your virtual envirnoment (in this case .venv/), like this:

pip install requests

If you don't want to activate and deactivate virtual environments, you can run pip and python directly from the virtual environment, like this:

$ .venv/bin/pip install requests
$ .venv/bin/python
>>> import requests
>>>

Or: 3. Use pipx

pipx is a great tool for install command-line applications straight from PyPI. To install pipx, run:

sudo apt install pipx

Make sure ~/.local/bin/ is in your PATH environment variable, by running this command:

pipx ensurepath

Close your terminal and open it again for the changes to take effect.

Now you can install a command-line application from PyPI. Behind the scenes, pipx will set up a virtual environment for each application, with its dependencies, completely isolated from the rest of the system to prevent problems. It's brilliant. Here's an example:

$ pipx install ruff
$ ruff --help

Or: 4. Pass --break-system-packages

If you absolutely must eat broken glass, then you can pass the --break-system-packages option, like this:

pip install --break-system-packages requests

Never remove or rename /usr/lib/python3.12/EXTERNALLY-MANAGED. It is there to stop you from breaking your system. You may not notice that your system is broken until weeks or months later, and you won't understand at that point why it broke. If you must ignore these protections, you can do it on a one-off basis using --break-system-packages.

10
  • I agree with the sentiment expressed in this post. The only time I used --break-system-packages option was to install pipenv, which is what I use to create a virtual environment. pipenv from official distribution can be quite old. Once you have a virtual environment, you're good to install anything else you need without worrying about "externally managed environment".
    – Dr Phil
    Commented Jul 17 at 13:43
  • 1
    @DrPhil If you need the latest version of pipenv, I would install it using pipx, like this: pipx install pipenv . This installs pipenv in its own isolated virtual environment behind the scenes. You don't need to use --break-system-packages in this case. As for pipx, you can install it using sudo apt install pipx.
    – Flimm
    Commented Jul 17 at 14:31
  • 2
    When I'm adding --prefix=/usr/local to pip install, I know exactly what sort of glass I'm eating — and I'm well ready to take it. This answer is good tho.
    – ulidtko
    Commented Aug 5 at 11:08
  • pipx requires +119Mb extra packages... But is a good alternative.
    – Kaymaz
    Commented Aug 11 at 7:21
  • 1
    @Paul Indeed, Python does include venv in its releases since 3.3. However, for some reason that is beyond me, Debian goes out of its way to remove venv from the python3 packages on Debian and from the default installation of Python on Debian, and you have to do this extra step on Debian/Ubuntu to install venv.
    – Flimm
    Commented Oct 8 at 16:03
63

Just

python3 -m venv ~/.local --system-site-packages

Be sure ~/.local/bin is in your $PATH

Then use

~/.local/bin/pip install ... # --user is implied ;)

You could probably just create your own ~/py directory and initialize everything from there as well. However, I think .local is already picked up by PATH and import directories.

4
  • 1
    This is what I did, although using a newly-created directory in my home dir rather than .local, to avoid mixing a venv with whatever other binaries might end up in .local. Also might have to re-do the venv as python versions roll out, might as well have it isolated. Then I just added the new venv's binary to the start of PATH in .bashrc , re-did pip installs for dependencies I need, and old commands work like they used to. Commented Aug 1, 2023 at 22:38
  • That's a very good one for my way of working, but it's even better if you add the "--system-site-packages" option so you still have access to system libraries.
    – ThoSil
    Commented Sep 19, 2023 at 14:21
  • Got a link explained the venv thing. It uses a local folder instead of system folder to install python dependencies, to avoid harming. That's much same to Nodejs and Java devel.
    – cornnutz
    Commented Dec 12, 2023 at 3:48
  • If you're going to specify the path explicitly anyway, then it doesn't matter whether you use a directory that's already on PATH. (And if you don't want to specify the path explicitly, then it will depend on which appears first in PATH.) By "import directories" I assume you mean the default configuration of sys.path. If you're using a virtual environment, this will automatically include the virtual environment's site-packages, no matter where you create it. Commented Nov 5 at 3:13
46

I installed pipx first:

apt install pipx

Then I used pipx to install radian:

pipx install radian

Later to confirm the installation location (in my case to configure Visual Studio Code), I ran:

pipx list
3
  • 20
    Why / how does this work? What does pipx do? Commented Sep 6, 2023 at 20:42
  • 16
    This answer does not really answer the question, also, pipx is not a replacement for pip, because pipx only can install applications, it cannot install system-wide Python libraries. That said, using pipx when possible is still a good advice and may help to keep the system clean. In my case, I ended up doing sudo rm /usr/lib/python3.*/EXTERNALLY-MANAGED to allow myself to use pip normally, but this should be considered a last resort, when alternative solutions either did not work or turned out to be not practical. Commented Oct 8, 2023 at 19:49
  • 3
    pipx is fundamentally intended for installing applications, not libraries. Relatively few Python applications are set up in a way where it would make sense to obtain them with Pip in the first place; and if you want to use a library in your own code then pipx will not help - it's there to create a separate virtual environment that's intended specifically and privately for the installed application. Commented Jun 22 at 5:30
34

Set this environment variable in your shell (ex bash):

export PIP_BREAK_SYSTEM_PACKAGES=1

Or write it in your Dockerfile:

ENV PIP_BREAK_SYSTEM_PACKAGES 1

Reference: Python 3.11, pip and (breaking) system packages

1
  • 1
    Environment? Do you mean environment variable? Commented Mar 14 at 3:03
16

# rm /usr/lib/python3.11/EXTERNALLY-MANAGED

1
  • 13
    PEP 668 was created for a valid reason. It's unwise to ignore it.
    – micmalti
    Commented Aug 20, 2023 at 18:44
16

Use:

  1. Open Terminal

  2. Run sudo nano /etc/pip.conf

  3. Add following line:

    [global]
    break-system-packages = true
    
  4. Ctrl + X then Y → press Enter (perform save in the nano editor)

Everything is updated now you can run pip install <package_name>.

4
  • sudo nano /etc/pip.conf ...
    – Galacticai
    Commented Oct 24, 2023 at 18:20
  • "Terminal" is ambiguous. Do you mean a terminal or the Mac application? Commented Mar 14 at 0:39
  • for macOS, brew, Python 3.12 the pip.conf file is at: nano /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.12/pip.conf
    – Mna
    Commented May 28 at 12:01
  • 3
    This was already explained in the top answer, and in a safer way (editing the user-level Pip configuration makes more sense; making the change in the system configuration is completely unnecessary). And really - do programmers need to be given step by step instructions for how to edit a text file? For that matter, there are probably systems out there without nano. Commented Jun 22 at 5:33
14

works for all - Windows, Linux, MacOS, Android ,PI SOLUTION (Put this at the end of your pip command) :- --break-system-packages

pip install package-name --break-system-packages 
0
12

Python is like hell for system administrators... Different software use different versions of many different things.

A few times, I've used pip3 to install things... that break other things. Sometimes I mix it with "apt-get install".

This error message is just like heaven... because it forces us to do the things right. It means the package manager (Ubuntu, Debian) is responsible for handling dependencies, not pip.

It's why we have Conda, or Miniconda.

You can create an environment using something like

conda create --name thenameofmyapp python=3.8

Activate your environment using

conda activate pixray

Then you can "pip install -r requirements.txt" and it will not break your system :) It will just install things in a specific environment.

4
  • virtualenv is pretty much a complete solution too (And seriously any python dev that doesnt know virtualenv or one of its variants [personally Im a fan of Pipenv] isnt keeping up at all. Reproducability! If it runs in a virtualenv, you can run it in a docker container, and if you can run it in a docker container, your sysadmins will like it very much indeed.
    – Shayne
    Commented Sep 16, 2023 at 16:40
  • 7
    It shows this error in a conda environment as well, did you try this yourself before writing? Commented Nov 2, 2023 at 20:06
  • 1
    This answer is misleading. It throws the same error within Conda (on Ubuntu at least).
    – ahron
    Commented Jun 21 at 18:18
  • 1
    This is a terrible idea. Even if it worked around the error message, trying to use Pip within a Conda environment is known to cause man problems. And Conda is a "Python-specific package manager"; so if it's respecting the spec then it will still not install in the system environment (other comments here suggest that others expect it to). Commented Jun 22 at 5:21
11

That issue is from pip. Just run the command and it will downgrade it.

pip install pip==22.3.1 --break-system-packages

Surely that will help.

2
  • 5
    This may get rid of the symptom, but what is the risk? "--break-system-packages" sounds scary. Can you provide an explanation, please? From another answer: "Your distribution is trying to protect you against mixing apt provided packages and pip provided packages. Mixing two package managers (apt and pip here) is always a bad idea" Commented Sep 7, 2023 at 10:11
  • 1
    So... use the system as intended, specifically for the purpose of rolling back Pip to a version that doesn't implement that system? Without any consideration of the system's purpose? Really? Not to mention everything else that you get with continued Pip updates? Commented Jun 22 at 5:35
4

In my case, when I was trying to install mkdocs-material this error happened and the solution to it was:

Linux/MAC:

$ python3 -m venv venv
$ source venv/bin/activate
(venv) $ pip3 --version
pip 21.2.3 from .../python3.10/site-packages/pip (python 3.10)
(venv) $ pip --version
pip 21.2.3 from .../python3.10/site-packages/pip (python 3.10)

Windows:

C:\> python -m venv venv
C:\> venv\Scripts\activate.bat
(venv) C:\>  pip3 --version
pip 21.2.3 from ...\lib\site-packages\pip (python 3.10)
(venv) C:\>  pip --version
pip 21.2.3 from ...\lib\site-packages\pip (python 3.10)

Please read: https://realpython.com/what-is-pip/#using-pip-in-a-python-virtual-environment

4

I was trying to install virtualenv and virtualenvwrapper to a fresh Linux Mint 22 with pip:

sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper

and I got this error:

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.12/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 did as it suggested:

To install Python packages system-wide, try apt install python3-xyz, where xyz is the package you are trying to install.

so this has worked:

sudo apt install python3-virtualenv
sudo apt install python3-virtualenvwrapper
3
  • Please read existing answers before contributing your own. This adds no new information. Commented Nov 5 at 3:16
  • I did read the existing answers and none worked for me. I commented the one that worked for me, and it's not posted anywhere here. You can CTRL+F for "install python3-virtualenv" if in doubt. And it seems it has helped some people, since it has 4 upvotes. Your comment (and mine) was just a waste of our time.
    – babis21
    Commented Nov 5 at 8:06
  • What happened when you tried using the standard library venv instead of virtualenv? Commented Nov 5 at 17:21
3

Do the following:

cd /usr/lib/python3.11

sudo rm EXTERNALLY-MANAGED

If you choose to restore this mechanism, create the same file again with the touch command:

sudo touch EXTERNALLY-MANAGED
3
  • 4
    PEP 668 was created for a valid reason. It's unwise to ignore it.
    – void
    Commented Jan 30 at 13:25
  • 1
    Although the file only needs to be present to trigger the error, it contains useful information that Pip will show to help with debugging (basically, everything under the arrow in the OP's example, except it's an INI file that also allows for localization). Deleting the file and later replacing it with a blank is inferior, as that information is lost. Better to rename it and later move it back. But really, there is no good reason to disable the warning. Commented Jun 22 at 5:24
  • 2
    Aside from that, w3sic3's answer already covers this material. Commented Jun 22 at 5:39
3

To fix that error, you can use a Python virtual environment. Here is how you can do that.

Install a Python virtual environment

Then you can move into a directory that you wish and create a virtual environment using virtualenv your_folder_name. While in the environment you have created, type source bin/activate.

Here is an easy way (video).

Then that is it.

1
  • There's also source bin/activate.fish if you use fish shell Commented Mar 2 at 3:20
3

Working with virtual environment.

Create a Virtual Environment:

$ python3 -m venv ~/myVirtualEnv

Access Virtual Environment directory:

$ cd ~/myVirtualEnv

Activate (start) this Virtual Environment

$ source bin/activate

-> your shell change to something like:

(myVirtualEnv) jose@nigriventer:~/myVirtualEnv$ 

If you are into myVirtualEnv you can run pip3 directly to install packages. Of course, this packages will stay "locked" into this virtual environment.

(myVirtualEnv) jose@nigriventer:~/myVirtualEnv$ pip3 install adafruit-ampy
2

Try these to avoid externally managed env:

  1. python3 -m venv path/to/venv.
  2. source path/to/venv/bin/activate
  3. python3 -m pip install xyz(any extension)
1

I ran across this problem while running some tasks on a pipeline job that uses the Docker image. I hadn't had this problem before, but since I am not using any specific tag for the Docker image, I am also not very surprised.

I was running the command:

python3 -m pip install --upgrade pip

which I replaced with:

python3 -m pip install --upgrade pip --break-system-packages

And things worked as usual.

1
  • 2
    Please read existing answers before posting your own. A simple question like this doesn't need dozens of answers. Commented Jun 22 at 5:40
1

Context: I am ultimately building a Node.js image with a C++ dependency (node-libcurl), but I need python3 installed in order for node-gyp to build that C++ dependency for multiple architectures. I wrote this answer because too many other answers are taking the easy route of disabling the warning, where you risk punting a problem down the road to bite you later.

My Dockerfile was working fine until a recent build of python3 where setuptools was no longer explicitly included in apk install. This Dockerfile was working last year, but had since started to break. It simply wouldn't build. The node-gyp package uses gyp in python3, and it simply wasn't there due to a missing setuptools package.

ARG NODE_TAG=20-alpine
ARG PLATFORM_ARCH=linux/amd64

FROM --platform=${PLATFORM_ARCH} node:${NODE_TAG} as devBuild

WORKDIR /home

# ------------------
# dev dependencies
# ------------------
# node-libcurl build from source (works on x64 / arm64 as of Oct 2023)
RUN apk add --update --no-cache libcurl python3 alpine-sdk curl-dev \
  && npm install node-libcurl --build-from-source \
  && rm -rf /var/cache/apk/*dil
# ----------------

COPY package.json ./
COPY yarn.lock ./
RUN yarn install

# ... and so on

Based on this SO answer, I could see that getting setuptools was the solution. So I tried python3 -m pip install setuptools:

# ------------------
# dev dependencies
# ------------------
# node-libcurl build from source (works on x64 / arm64 as of Oct 2023)
RUN apk add --update --no-cache libcurl python3 alpine-sdk curl-dev \
  && python3 -m pip install setuptools \
  && npm install node-libcurl --build-from-source \
  && rm -rf /var/cache/apk/*dil
# ----------------

But it wasn't working, and I was getting the above message about an "externally-managed-environment", which led me to here on SO.

This message was advising me to use apk to install setuptools, since python3 was controlled by that tooling. So I finally realized (thanks to hints from other answers here) that I should add py3-setuptools in the dev dependencies section:

# ------------------
# dev dependencies
# ------------------
# node-libcurl build from source (works on x64 / arm64 as of Oct 2023)
RUN apk add --update --no-cache libcurl python3 py3-setuptools alpine-sdk curl-dev \
  && npm install node-libcurl --build-from-source \
  && rm -rf /var/cache/apk/*dil
# ----------------

And now I'm able to build my Node-based Docker image for both x64 and arm architectures again!

1
  • 2
    This will only work for whichever packages are actually made available by the "external management". It's also directly explained in the error message in the first place. Commented Jun 22 at 5:51
1

I was trying to get Ansible setup on Debian/Ubuntu when this error popped up, and here's the simplified answer to the solution that worked for me:

# Install venv if you don't have already. 
# Folder here .venv for me, but can be anything you choose
python3 -m venv .venv

# Now, lets use that virtual environment to install Ansible (or any package of your choice)
.venv/bin/pip3 install ansible

# Let's activate the virtual environment
source .venv/bin/activate

# Quick test, and congratulations! You got Ansible now! :-)
ansible --version

Thanks for others' answers. Sorry, pipx didn't quite worked for me, and venv did, and must admit I may be biased towards venv as I use it often). Also didn't want to enable the --break-system-packages option.

My comment is just to share my experience, and to simplify a quick course of action.

0
0

One other way to install packages that are not available via the distribution's package manager may be pip's prefix option, as documented at packaging.python.org

pip install --prefix=/some/path

Calls sysconfig.get_preferred_scheme('prefix').

The prefix is distribution dependent. E.g., Debian uses /usr/local for packages not installed via the system package manager.

Some gotchas are also possible. On Devuan (so possibly also on Debian itself and other derivatives), the prefix needed to target /usr/local is /usr:

pip install --prefix=/usr some_package

installs some_package in /usr/local where it is visible to applications installed by the system package manager.

However,

pip install --prefix=/usr/local some_package 

installs some_package in /usr/local/local, which does not work.

0

I had this problem while trying to use pip from inside a virtual environment on python 3.12.

Probably some guardrail to avoid installing packages on system-wide python by mistake (throw the first rock the one who never did it before).

So after loading your venv

source venv/bin/activate

you should use your pip by pointing to the venv directory or by using the activated python with the module flag

./venv/bin/pip install xyz
# or
python3 -m pip install xyz
1
  • Please read existing answers before contributing your own. This contains nothing that isn't extensively covered by the top answers. (Also, while the venv is active, pip will already "point to the venv directory": the main thing that "activating" the venv does is to put that ./venv/bin path on the PATH. Which is the same reason that python3 -m pip works: it now refers to ./venv/bin/python3.) Commented Nov 5 at 3:21
0

Another option nobody mentioned is to install from source! No need for pip or virtual environments. Simply download source and python setup.py install. I find this the best and less tedious way, especially that I use many modules that aren't provided by apt. Also, this approach works nicely with Dockerfile.

0

I searched among the answers, and nobody tried the following approach.

I had the same error in my Spyder in Ubuntu 24.04. However, I've noticed that if I run Python on the terminal, I can import the installed packages. I've changed the python interpreter in Spyder from "Tools">"Preferences">"Python interpreter" setting the address to /usr/bin/python3 that I can find from the terminal with the command which python3

After restarting, Spyder asks to install spyder-kernels==3.0.*, so I run from terminal

python3 -m pip install --break-system-packages spyder-kernels==3.0.*

Now it works.

0

If you use conda, simple conda install pip solves the issue.

-1

I am not sure about the author's environment and which package they're trying to install, but perhaps this will help someone else.

I just got this error while using the Python extension for Visual Studio Code. It requires the installation of Pylint in WSL, and when attempting to do it, I got the same error. This can be resolved by installing Pylint using APT:

sudo apt install python3-pylint-common
-1

Take a look at this. It fixes it without using venv in Bash:

  1. Open Terminal on Mac, and make sure your shell is Bash

  2. Type nano ~/.bash_profile

  3. Use arrow keys to go to the bottom of bash_profile

  4. Paste export PATH=".:/Library/Frameworks/Python.framework/Versions/3.12/bin:/usr/local/bin:${PATH}" onto the bottom of bash_profile

  5. Type Control + X, press Y, and then Enter

  6. Type source ~/.bash_profile

  7. Enjoy!

1
  • 1
    This tells the system to use a different installed version of Python when the python command is typed in. That could break other things, and could only possibly work if you actually have that other Python installation in that specified location. It's not a real solution to the question as asked and it fundamentally misunderstands the problem. Commented Jun 22 at 5:43
-1

If you are in a virtual environment (which is recommended), sudo pip install .... will cause the "externally managed"-error. As "sudo" means system-wide installation, this seems to be a logical consequence.

I ran into this problem a few hours ago, being used to "sudo" when working on a project years ago.

pip install ...

within a virtual environment is the right thing to do.

Hope this helps anybody who is as little experienced as I am.

2
-2

If you're using venv and you still get this error, try

pip cache purge
1
  • 1
    If you get this error "using venv" then you aren't actually using it properly. Pip will explicitly check whether the code is running from a virtual environment - it has to, since otherwise it would always detect the marker file, since the virtual environment is dependent upon the base environment to provide the standard library. Commented Jun 22 at 5:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.