Understanding .pth in site-packages
OKB (not okblacke)
brenNOSPAMbarn at NObrenSPAMbarn.net
Sun Aug 28 07:20:23 CEST 2011
Josh English wrote:
> The setup.py script created the egg, but not the .pth file. I
> created that myself.
> Thank you for clarifying about how .pth works. I know "redirect
> imports" was the wrong phrase, but it worked in my head at the
> time. It appears, at least on my system, that Python will find
> site-packages/foo before it finds and reads site-packages/foo.pth.
> At least this solution gives me a way to develop my libraries
> outside of site-packages.
Well, I'm still not totally sure what your setup is, but assuming
site-packages/foo is a directory containing an __init__.py (that is, it
is a package), then yes, it will be found before an alternative package
in a directory named with a .pth file. Note that I don't say it will be
found before the .pth file, because, again, the finding of the package
(when you do "import foo") happens much later than the processing of the
.pth file. So it doesn't find site-packages/foo before it reads
foo.pth; it just finds site-packages/foo before it finds the other foo
that foo.pth was trying to point to.
Let's say your .pth file specifies the directory /elsewhere. The
.pth file is processed by site.py when the interpreter starts up, and at
that time /elsewhere will be appended to sys.path. Later, when you do
the import, it searches sys.path in order. site-packages itself will be
earlier in sys.path than /elsewhere, so a package site-packages/foo will
be found before /elsewhere/foo. The key here is that the .pth file is
processed at interpreter-start time, but the search for foo doesn't take
place until you actually execute "import foo".
If you want to make your /elsewhere "jump the line" and go to the
front, look at easy_install.pth, which seems to have some magic code at
the end that moves its eggs ahead of site-packages in sys.path. I'm not
sure how this works, though, and it seems like a risky proposition.
--OKB (not okblacke)
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
More information about the Python-list