Why does `import site` fail to load site-specific paths under python3?

Viewed 39

Python2's import site works correctly, but Python3's does not. Use case: uwsgi running as a specific user, so requires no-site in .ini file. Symptom of problem: ModuleNotFoundError attempting to import lxml, which is under /usr/lib/python3/dist-packages.

jcomeau@bendergift:~$ python -S
Python 2.7.16 (default, Apr  6 2019, 01:42:57) 
[GCC 8.3.0] on linux2
>>> import sys
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload']
>>> import site
>>> sys.path
['/home/jcomeau', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/jcomeau/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/wx-3.0-gtk3']

>>> 
jcomeau@bendergift:~$ python3 -S
Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.3.0] on linux
>>> import sys
>>> sys.path
['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload']
>>> import site
>>> sys.path
['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload']
2 Answers

It works as expected:

python -S

https://docs.python.org/3/library/site.html

For Python versions >= 3.3:

Importing this module will append site-specific paths to the module search path and add a few builtins, unless -S was used. In that case, this module can be safely imported with no automatic modifications to the module search path or additions to the builtins.

For Python versions < 3.3:

Changed in version 3.3: Importing the module used to trigger paths manipulation even when using -S.

The fix, as fuzzy drawings's link indicates, was to call site.main():

if tuple(sys.version_info) >= (3, 3):
    site.main()
Related