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
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 tosys.path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With