Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 911174 - emerge raises a KeyError: 'PKGDIR' exception when executed after installed from pip on a non-gentoo distro
Summary: emerge raises a KeyError: 'PKGDIR' exception when executed after installed fr...
Status: CONFIRMED
Alias: None
Product: Portage Development
Classification: Unclassified
Component: Core (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Portage team
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-07-24 21:39 UTC by Daan De Meyer
Modified: 2023-07-30 10:31 UTC (History)
1 user (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daan De Meyer 2023-07-24 21:39:36 UTC
Portage crashes and shows no informative error message when executed on another distro after being installed with pip install --user

Reproducible: Always

Steps to Reproduce:
1. pip install --user portage
2. PYTHONPATH=$HOME/.local/lib/python3.11/site-packages ~/.local/lib/portage/bin/emerge
Actual Results:  
```
======== Traceback for invalid call to portage.package.ebuild.config.config.__getitem__ ========
  File "/home/daandemeyer/.local/bin/emerge", line 59, in <module>
    retval = emerge_main()
  File "/home/daandemeyer/.local/lib/python3.11/site-packages/_emerge/main.py", line 1261, in emerge_main
    emerge_config = load_emerge_config(action=myaction, args=myfiles, opts=myopts)
  File "/home/daandemeyer/.local/lib/python3.11/site-packages/portage/proxy/objectproxy.py", line 31, in __call__
    return result(*args, **kwargs)
  File "/home/daandemeyer/.local/lib/python3.11/site-packages/_emerge/actions.py", line 2900, in load_emerge_config
    emerge_config.trees = portage.create_trees(trees=emerge_config.trees, **kwargs)
  File "/home/daandemeyer/.local/lib/python3.11/site-packages/portage/__init__.py", line 714, in create_trees
    "bintree", binarytree, pkgdir=mysettings["PKGDIR"], settings=mysettings
  File "/home/daandemeyer/.local/lib/python3.11/site-packages/portage/package/ebuild/config.py", line 3116, in __getitem__
    return self._getitem(key)
           ^^^^^^^^^^^^^^^^^^
  File "/home/daandemeyer/.local/lib/python3.11/site-packages/portage/package/ebuild/config.py", line 3193, in _getitem
    raise KeyError(mykey)
KeyError: 'PKGDIR'
```
Comment 1 James Le Cuirot gentoo-dev 2023-07-30 08:50:20 UTC
I see the problem. It works when installed to a venv because sys.prefix returns the path to the venv, but the environment used by "pip install --user" is not a venv. We'd probably need to do something based on __file__ instead.
Comment 2 James Le Cuirot gentoo-dev 2023-07-30 10:31:29 UTC
I'm not sure that we can fix this properly. I don't want to rely on __file__ to go from foo/lib/python3.XX/site-packages/portage/const.py to just foo because this scheme can vary between platforms and distributions. pip has tons of code just to figure this stuff out. Distribution-managed environments and virtual environments are really the only sane options anyway. Unlike virtual environments, the environment used by "pip install --user" is designed to be used for installation of different modules, but that will inevitably lead to conflicts. It can even break your system-wide modules. This is why Gentoo slaps a big warning on it:

> $ pip install --user .                                                        
> error: externally-managed-environment
> 
> × This environment is externally managed
> ╰─> 
>     The system-wide Python installation in Gentoo should be maintained
>     using the system package manager (e.g. emerge).
>     
>     If the package in question is not packaged for Gentoo, please
>     consider installing it inside a virtual environment, e.g.:
>     
>     python -m venv /path/to/venv
>     . /path/to/venv/bin/activate
>     pip install mypackage
>     
>     To exit the virtual environment, run:
>     
>     deactivate
>     
>     The virtual environment is not deleted, and can be re-entered by
>     re-sourcing the activate file.
> 
> 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.