pythonpython-packaginggentoopython-poetry

How to use system installed packages?


I am trying to write script, which should use Portage API. But Portage python package doesn't available in PyPi, but even if it were available that would not make any sense, because it should be used from some configured system with package database and configs.

I tried to wrote the following ugly enough code:

[tool.poetry.dependencies]
python = "^3.6"

click = "^7.0-r1"

portage = [
{ markers = "python_version ~= '3.6' and sys_platform == 'linux'", path = "/usr/lib64/python3.6/site-packages/portage/" },
{ markers = "python_version ~= '3.7' and sys_platform == 'linux'", path = "/usr/lib64/python3.7/site-packages/portage/" },
{ markers = "python_version ~= '3.8' and sys_platform == 'linux'", path = "/usr/lib64/python3.8/site-packages/portage/" },
{ markers = "python_version ~= '3.9' and sys_platform == 'linux'", path = "/usr/lib64/python3.9/site-packages/portage/" }
] 

But it doesn't work. Code in the path directory doesn't considered by Poetry as python package.

[ValueError]
Directory /usr/lib64/python3.6/site-packages/portage does not seem to be a Python package

Is there a way to do this and to use system as virtual environment for running tests (I understand that running tests in host system isn't a good idea, but there is a docker image with installed Portage)?


Solution

  • Looks to me like it could be related to the virtual environment not allowing access to the system site packages. If it is indeed the case then note that this is not feasible with poetry as of today. There is an open issue, as well as a pull request though.

    A workaround might be to create the virtual environment without poetry first, for example:

    path/to/python3 -m venv --system-site-packages .venv
    

    and then use poetry from within this virtual environment, since poetry is supposed to not create a virtual environment when it can detect that it is running from within one and will use it instead.


    Apparently portage is not a pip-installable Python project, so it is most likely useless to specify markers and path. Additionally site-packages directories usually contain the installed projects, whereas path should point to a location from where poetry (pip) can download an installable distribution of the project.

    I believe once the issue with --system-site-packages is resolved then portage could be listed as a simple dependency portage = "*".