0

This is a new Pi 5 running Raspberry Pi OS and Python 3.11.

I have a dependency on Pillow. Definitely not PIL because PIL lacks certain features - specifically ImageTk: ImportError: cannot import name 'ImageTk' from 'PIL'.

pip install pillow returns error: externally-managed-environment and tells me to install using sudo apt install python3-pillow. That, in turn says Note, selecting 'python3-pil' instead of 'python3-pillow' - which is easily missed and leads to the original error.

So what's the official way to get Pillow on a Pi? On an old Pi3 for testing I used the --break-system-packages override, but I'm wary of doing that on a system that is intended to be maintained long term.

1 Answer 1

1

you might try to create a virtual environment and install pillow there.

python -m venv ~/myvenv
source ~/myvenv/bin/activate
pip install -U pip
pip install pillow

then use ~/myvenv/bin/python to run the script requiring pillow.

Does this work or do you get the same kind of error message (error: externally-managed-environment )

I'm no expert concerning the "externally-managed-environment" in fact I read it the first time in your post. But a small search seems to indicate that this is a recent enhancement that tries to avoid conflicts between packages installed with apt and packages installed with pip.

So my suggestion to use a venv doesn't seem that bad.

I read https://itsfoss.com/externally-managed-environment/ and it says amongst many other things:

Ubuntu 23.04, Fedora 38 and probably other recent distribution versions are implementing this enhancement on the use of Python packages.

The change has been done to avoid the "conflict between OS package managers and Python-specific package management tools like pip. These conflicts include both Python-level API incompatibilities and conflicts over file ownership."

What are your options?

You can do three things when you try to install a Python package and see this error.

Install the native package
Create virtual environments in Python
Use Pipx (recommended)

I never tried the option pipx (but I it seems it will also create a virtualenv, + some more automatic stuff) However I think pipx works only for executables and not for libraries, so I'd stick with a vinv or a virtualenv at least utnil pipx is better understood

6
  • That's certainly a valid approach, though for a device that's going to spend its life running one script it seems like an extra layer of complexity. I'll test this, and another approach, on the second Pi when I get the chance (it's for work but setup and testing is much easier at home)
    – Chris H
    May 15 at 6:10
  • a venv is an easy approach to have a python environment with predictable packages. you might check whether you installed perhaps the apt-package python-pil and whether it is this package creating conflicts.
    – gelonida
    May 15 at 7:58
  • did you run pip install pillow as root user or as another user. If running as another user you might try the argument --break-system-packages in the pip command. However I strongly recommend to not (never) do this as root user.
    – gelonida
    May 15 at 8:00
  • 1
    I slightly enhanced the answer
    – gelonida
    May 15 at 8:15
  • did you have time to try? or did you find a better solution? If yes, then perhaps post yours.
    – gelonida
    May 19 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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