python: Interplay between lib/site-packages/site.py and lib/site.py

前端 未结 1 1830
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 03:24

Due to a specific problem which I managed to solve, I spent most of today figuring out how site.py(s) work. There is a point which I don\'t understand.

As far as I u

相关标签:
1条回答
  • 2020-12-11 04:22

    The lib/python2.7/site-packages/site.py file is not normally loaded. That's because it is lib/python2.7/site.py's job to add the site-packages paths to sys.path to begin with, and the site.py in site-packages is simply not visible. If you have a site.py in site-packages then that is an error, there should be no such file there.

    What happens in an unpatched Python without is:

    • Python starts, with a limited sys.path. site-packages is not part of this list, unless you set a PYTHONPATH variable that includes it anyway.
    • Python imports site.py, it'll import the one listed first on sys.path.
    • lib/python2.7/site.py is found and loaded
    • site.py adds site-packages to sys.path

    That's it, no further site.py modules are loaded. Even if you tried, it'd find the module that was already imported; sys.modules['site'] exists and holds objects loaded from lib/python2.7/site.py.

    Your installation, however, has an older setuptools installed, and it includes a special version of site.py, which the easy_install command will install into site-packages if not yet present. It'll load the original site.py by explicitly scanning the original sys.path with any PYTHONPATH-supplied paths ignored and loading the original site.py module manually using the imp.find_module() and imp.load_module() low-level functions, thus bypassing the normal module cache.

    Its intent was to alter the sys.path order to give PYTHONPATH-listed .pth files a higher precedence, see the original commit adding the patch:

    Note: this version includes a hacked 'site.py' to support processing .pth files in directories that come before site-packages on sys.path.

    The patch has been removed entirely from more recent setuptools releases, as early as 2006 in the original setuptools.

    So, either your Linux distribution has been set up to add lib/python2.7/site-packages to your PYTHONPATH or your shell has this set up for you, or your Python has been patched to include it, and you have the old setuptools 'patch' in your site-packages.

    It is entirely safe to remove that file.

    0 讨论(0)
提交回复
热议问题