ridiculous_fish
ridiculous_fish

Reputation: 18541

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

Upvotes: 4

Views: 8500

Answers (2)

indirectlylit
indirectlylit

Reputation: 451

This doesn't answer why, but adding this line to the Dockerfile seems to work around the issue:

RUN echo "/usr/lib/python3.x/site-packages" >> /usr/local/lib/python3.x/dist-packages/site-packages.pth

where x is your Python minor version.

This workaround is based on the Modifying Python’s Search Path section of the docs which says:

The most convenient way [to add a directory] is to add a path configuration file to a directory that’s already on Python’s path ... Path configuration files have an extension of .pth, and each line must contain a single path that will be appended to sys.path.

Upvotes: 2

Bjoern Dahlgren
Bjoern Dahlgren

Reputation: 930

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")

Upvotes: 0

Related Questions