1002

When I run pip install xyz on a Linux machine (using Debian or Ubuntu or a derived Linux distribution), 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?

6

44 Answers 44

706

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), 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.

Sign up to request clarification or add additional context in comments.

15 Comments

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?
@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.)
@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.
This change in PEP 668 is somewhat ill-conceived. It breaks a lot of things, and there is no easy fix. There are many instances where one would like to install python libs and applications globally. If upgrading from a previous version, all these packages are broken. In my case, I was unable to successfully install ipython either with pipx or apt install. Previous recommended practice was to always use pip to install python packages, not apt. I have never had a conflict problem with this approach. Setting global.break-system-packges true is what I recommend if you follow this practice.
Installing Python packages in a Docker environment is such an exception or not? Because building a venv inside a Docker container which is already an isolated environment is like Inception to me. And it's nowadays quite painful to meet this error when building Docker images based on Python.
|
348

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 Comments

Just beware that this may still break your system
rm vs mv ..just in case you would like to revert back.
This file does not exist on mac -- what is the mac/brew equivalent?
@JackLeEmmerdeur Even better: sudo mv /usr/lib/python3.11/EXTERNALLY-MANAGED /etc/motd :)
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
|
190

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 Comments

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.
Could this approach have any potentially negative consequences if used only to install a single package while building a docker image?
@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.
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...
182

Currently, some of the most upvoted answers are teaching you ways to ignore this problem. This is like telling you to take painkillers 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 environment (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 installing 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.

17 Comments

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".
@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.
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.
pipx requires +119Mb extra packages... But is a good alternative.
@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.
|
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 Comments

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.
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.
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.
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.
51

Set this environment variable in your shell (for example, 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

Comments

47

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 Comments

Why / how does this work? What does pipx do?
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.
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.
20

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 Comments

sudo nano /etc/pip.conf ...
"Terminal" is ambiguous. Do you mean a terminal or the Mac application?
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
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.
17

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

1 Comment

PEP 668 was created for a valid reason. It's unwise to ignore it.
13

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.

3 Comments

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"
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?
A similar answer (for another question).
13

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 Comments

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.
It shows this error in a conda environment as well, did you try this yourself before writing?
This answer is misleading. It throws the same error within Conda (on Ubuntu at least).
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).
12

This a works-for-all—Windows, Linux, macOS, Android, and Raspberry Pisolution (put this at the end of your pip command): --break-system-packages

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

Comments

5

Create .venv

python3 -m venv .venv

Then you have to use pip inside your .venv folder

.venv/bin/pip install -r requirements.txt

Comments

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

In the venv session:

pip3 --version
pip --version

Output:

pip 21.2.3 from .../python3.10/site-packages/pip (python 3.10)
pip 21.2.3 from .../python3.10/site-packages/pip (python 3.10)

Windows:

python -m venv venv
venv\Scripts\activate.bat

In the venv session:

pip3 --version
pip --version

Output:

pip 21.2.3 from ...\lib\site-packages\pip (python 3.10)
pip 21.2.3 from ...\lib\site-packages\pip (python 3.10)

Please read: Using pip in a Python Virtual Environment

Comments

4

Working with a virtual environment.

Create a virtual environment:

python3 -m venv ~/myVirtualEnv

Access the virtual environment directory:

cd ~/myVirtualEnv

Activate (start) this virtual environment:

source bin/activate

Your shell changes to something like:

(myVirtualEnv) jose@nigriventer:~/myVirtualEnv$

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

pip3 install adafruit-ampy

Comments

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 Comments

Please read existing answers before contributing your own. This adds no new information.
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.
What happened when you tried using the standard library venv instead of virtualenv?
4

I was trying to get Ansible set up 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 it already. 
# The folder here is .venv for me, but it can be anything you choose
python3 -m venv .venv

# Now, let’s 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 I didn't want to enable the --break-system-packages option.

Comments

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 Comment

There's also source bin/activate.fish if you use fish shell
3

Try these to avoid an 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)

Comments

2

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 Comments

PEP 668 was created for a valid reason. It's unwise to ignore it.
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.
Aside from that, w3sic3's answer already covers this material.
2

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

1 Comment

Yep, I forgot to include 'python=x.x.x' in my 'conda create' command.
2

For the case where you've requested the Python Feature as an add on to a Dev Container, it turns out that this problem can be solved simply by explicitly setting the Python version.

With the default "version", pip install fails:

"features": {
    "ghcr.io/devcontainers/features/python:1": {}
}

With an explicit "version", pip install succeeds:

"features": {
    "ghcr.io/devcontainers/features/python:1": {
        "version": "3.13"
    }
}

By making this change to ".devcontainer.json", you avoid the requirement of concerning yourself with the complexities of Python Virtual Environments inside your development containers.


Some explanation of why this works

The issue, in this case, is that the default value for "version" is "os-provided" which is natively incompatible with pip install as described in PEP 668. Overriding "version" will result in a separate "/usr/local" install of Python, which doesn't suffer from the problem. When interacting with the "local" version, pip install will manage packages in the "site-packages" folder along with the local install.

Note: The selection of which installed Python is used for your development work comes down to how Python is invoked inside of your container.

2 Comments

Can also use: { "version": "latest" }
Of course, using "latest" assumes you trust in the stability of Python. Version 3.14 recently came out and my "latest" dev container is failing to start. So "latest" is currently failing due to circular reference. <wink>
1

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.

Comments

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 Comment

Please read existing answers before posting your own. A simple question like this doesn't need dozens of answers.
1

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.

1 Comment

This worked like a charm for installing a library. It is also globally available. I had to install pyhon3-setuptools in Debian which just worked. Downside is that I have no idea what I did and why this is so easy. Did I work around something? Or is installing from source ignoring the need for --break-system-packages option?
1

I started getting this error inside a virtual environment when I tried to modify it after upgrading my OS (Ubuntu). In this case, the solution was to recreate the virtual environment.

(venv) $ deactivate
$ rm -r venv/
$ python3 -m venv venv
$ source venv/bin/activate
(venv) $ pip install -e .
(venv) $ pip install <the package I want>

Comments

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 Comment

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.
1

This is just a specific case, but it may be helpful for someone:

I had a virtual environment on Linux for Python 3 and pip and still got the message above.

The virtual environment was used and did work for Python 3, but not for pip.

The reason for this was that the directory for the virtual environment was named venv and not .venv.

Comments

0

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

I had the same error in my Spyder in Ubuntu 24.04 (Noble Numbat). However, I've noticed that if I run Python in 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.

Comments

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 Comment

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.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.