Forum Archive

About the PYTHONPATH envvar

dgelessus

Normally the PYTHONPATH environment variable can be used to add additional directories in front of the default import path. By default it is empty or not set, and the default import path is always added after any locations from PYTHONPATH. On Pythonista, PYTHONPATH contains all locations from the default import path, as if there were no default import locations and all locations were user-defined. It should really be emtpy, because all sys.path entries are provided by default, and none of them are set by the user.

Why does this matter? setuptools does some magic by installing a custom site.py under site-packages, which needs to find the real site.py module from the stdlib to work correctly. During this search it ignores any import locations from PYTHONPATH, which includes all import locations on Pythonista, meaning that nothing is searched at all and the import of site fails.

dgelessus

On a slightly related note, it looks like sys.path is reset every time a script is run (even an editor action) without reloading site again, which means that any previous sys.path changes from site (including .pth files) are lost until site is manually reloaded.