'site-packages directory not being added to sys.path?

My question is why /usr/lib/python3.8/site-packages/ is not being added to sys.path; I expect it to have been added by the site module.

Some details: from within a Docker container, I am using pip install -e to install some Python packages in editable mode. The packages get installed to /usr/lib/python3.8/site-packages; however this directory is not in sys.path so I cannot import them.

The site module docs say:

lib/pythonX.Y/site-packages... if it refers to an existing directory, and if so, adds it to sys.path. I've confirmed that this directory exists so I expect it to be added.

python3 -m site prints:

sys.path = [
    '/',
    '/usr/lib/python38.zip',
    '/usr/lib/python3.8',
    '/usr/lib/python3.8/lib-dynload',
    '/usr/local/lib/python3.8/dist-packages',
    '/usr/lib/python3/dist-packages',
]
USER_BASE: '/root/.local' (doesn't exist)
USER_SITE: '/root/.local/lib/python3.8/site-packages' (doesn't exist)
ENABLE_USER_SITE: True

thanks for any help.



Solution 1:[1]

I had the same problem for a python-3.8 installation (3.9 was fine), I added:

COPY usercustomize.py /root/

to the Dockerfile, with the contents of usercustomize.py:

import sys
if sys.version_info[:2] == (3, 8):
    sys.path.append("/usr/lib/python3.8/site-packages")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bjoern Dahlgren