Discussion about avoiding common pitfalls as developer

If I understand it right, for this to happen there has to be a file called EXTERNALLY-MANAGED in ‘/usr/lib/python3.11’. But in my stable Manjaro of 2023-06-04, EXTERNALLY-MANAGED does not exist there, and I can still do pip install --user without --break-system-packages.

At first I was puzzled by PEP 668,
as I think of ‘site-packages’ under /home/ as being NOT part of system python.
There’s plenty of argument about it here, where some people agree with that point of view.
If installing in /home/…/site-packages/ will be virtually banned,
I hope they’ll make it easier for the average user to set up and use a venv.

There was no such requirement on my system.

 $ pacman-mirrors -G
unstable

 $ inxi -S
System:
  Host: tiger Kernel: 6.3.8-1-MANJARO arch: x86_64 bits: 64
    Desktop: KDE Plasma v: 5.27.5 Distro: Manjaro Linux

Before I added the PEP668 reference I tested the use of pip on my system which produced these results

$ sudo pip install numpy
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try 'pacman -S
    python-xyz', where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Arch-packaged Python package,
    create a virtual environment using 'python -m venv path/to/venv'.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip.
    
    If you wish to install a non-Arch packaged Python application,
    it may be easiest to use 'pipx install xyz', which will manage a
    virtual environment for you. Make sure you have python-pipx
    installed via pacman.

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.
$ pip install --user numpy
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try 'pacman -S
    python-xyz', where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Arch-packaged Python package,
    create a virtual environment using 'python -m venv path/to/venv'.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip.
    
    If you wish to install a non-Arch packaged Python application,
    it may be easiest to use 'pipx install xyz', which will manage a
    virtual environment for you. Make sure you have python-pipx
    installed via pacman.

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.

However running the same command using the –break-system-packages argument produces a succes result - in this case the package exist - but otherwise it would have been installed.

 $ pip install --user numpy --break-system-packages
Requirement already satisfied: numpy in /usr/lib/python3.11/site-packages (1.24.3)

It hasn’t arrived in stable yet.
Thanks for the heads-up that it will be here soon.

I just realized - the file you mention is present - I just didn’t look for it until now - it holds the response I got.

 $ ls -l /usr/lib/python3.11/EXTERNALLY-MANAGED 
-rw-r--r-- 1 root root 553  5 jun 11:32 /usr/lib/python3.11/EXTERNALLY-MANAGED

 $ cat /usr/lib/python3.11/EXTERNALLY-MANAGED 
[externally-managed]
Error=To install Python packages system-wide, try 'pacman -S
 python-xyz', where xyz is the package you are trying to
 install.

 If you wish to install a non-Arch-packaged Python package,
 create a virtual environment using 'python -m venv path/to/venv'.
 Then use path/to/venv/bin/python and path/to/venv/bin/pip.

 If you wish to install a non-Arch packaged Python application,
 it may be easiest to use 'pipx install xyz', which will manage a
 virtual environment for you. Make sure you have python-pipx
 installed via pacman.

Now it makes sense.
Current pip in Arch is 23.2.1-1, since 2023-05-03, and it’s already in Stable.
Maybe the only difference in Unstable and Testing is
the presence of ‘/usr/lib/python3.11/EXTERNALLY-MANAGED’.
I added it in my Stable system and got the error message for sudo pip install.

But with --user, the message is a little different.

$ pip install --user httpx --dry-run
error: externally-managed-environment

× This environment is externally managed
╰─> The Python environment under /usr is managed externally, and may not be
manipulated by the user. Please use specific tooling from the distributor of
the Python installation to interact with this environment instead.

Don’t know where (on my system) the above different message is stored.
(Doesn’t matter, as I’m messing with Stable; on Unstable, Frede saw both messages the same.)
Note that it says “The Python environment under /usr is managed externally”
even though I’m trying to install under /home/.

If I remove ‘/usr/lib/python3.11/EXTERNALLY-MANAGED’ I get the old behaviour again.

EDIT: have added this topic to the guide here

EDIT2 (utc 230623 04:21) Clarified what I meant by “Don’t know where it’s from”.

python package :wink: (new alias : python-externally-managed)

https://packages.manjaro.org/?query=^python%24

From PEP668:

This applies both to system-wide installs (sudo pip install) as well as user home directory installs (pip install --user), since packages in either location show up on the sys.path of /usr/bin/python3.

The user home directory is not in sys.path when running as root or sudo,
therefore not for system processes also,
therefore nothing you do with pip install --user can “break the system”.
It might break some packages when you run “system python”, that’s all.
( If I understand it right … :slight_smile: )