Search code examples
pythonpip

How to apply `--break-system-packages` conditionally only when the system pip/python supports it?


I am adapting my pip commands to newer versions of Ubuntu (that support PEP 668). Out of the options, the only one worked so far (in my specific use case) is to

Use --break-system-packages at the end of pip

as indicated in this answer. That is, change

sudo pip install xyz

to

sudo pip install xyz --break-system-packages

. This worked for the newer versions of Ubuntu but causes an error in older versions of Ubuntu (22.04 LTS) that do not recognize the --break-system-packages option. The error message from pip is:

no such option: --break-system-packages

pip --version shows:

pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)

How can one add conditions around pip commands so that it uses the --break-system-packages option only when the pip/python version is high enough to recognize it?


Solution

  • You can set an environment variable instead of using --break-system-packages.

    PIP_BREAK_SYSTEM_PACKAGES=1 pip install xyz should work in both new and older versions of pip