PTH files: Abs paths not working as expected. Symlinks needed?

Steve D'Aprano steve+python at pearwood.info
Wed Feb 15 10:34:19 EST 2017


On Wed, 15 Feb 2017 11:42 pm, poseidon wrote:

> Yes, removed it (symlink still there) and it still works. But then, what
> are pth files for? 


Good question. I don't actually know anyone that uses pth files, so perhaps
they're unnecessary.

But the principle behind them is that they can be used to customize the
search path Python uses for locating modules. There are (at least) six ways
to customize this path:


(1) Have your application import sys and modify sys.path on startup.

(2) Set the environment variable PYTHONPATH.

(3) For those building and distributing their own Python environment, e.g.
Linux distros, you can customise the site.py file.

(4) For site-wide customization, create a sitecustomize.py file in (for
example) /usr/local/lib/python3.5/site-packages/ or the equivalent for your
system, and modify sys.path.

(5) For a per-user customization, create usercustomize.py in (for example)
~/.local/lib/python3.5/site-packages/ or the equivalent for your system.
Again, import sys and modify sys.path.

(6) Or place a pth file in one of the site-packages locations.

See the documentation for the site module for more detail:

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



> I'd just place a symlink to the package and am done 
> with. The path doesn't seem to be needed in sys.path (where it would go
> if placed in a pth file). If I write
> 
> from tau4 import datalogging
> 
> that works, too. So no need for the path being in sys.path (i.e. in a
> pth file)?

Where are you placing the symlink? Python will follow symlinks, but they
have to be in the PYTHONPATH to be found in the first place. And not all
platforms support symlinks. (Windows, I think.)




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list