Is there some way to set python's search path in a config file without setting PYTHONPATH, i.e. some default configuration file that python reads when it starts?
- 7,990
- 8
- 49
- 71
-
Related: [Creating a secondary site-packages directory (and loading packages from .pth files therein)](http://stackoverflow.com/q/10693706/95735) – Piotr Dobrogost May 02 '13 at 07:39
2 Answers
You have two options:
List additional paths in a
.pthfile in one of the standard locations (usually yoursite-packageslocation). See How to add a Python import path using a .pth file as well.Add additional paths to
sys.pathinsitecustomizeorusercustomizemodules (detailed in thesitemodule documentation). Yoursitecustomizeorusercustomizecould look something like:import sys sys.path[0:0] = [ '/foo/bar', '/spam/eggs', ]where the two extra entries would be inserted into
sys.pathat the front.You can also call
site.addsitedirwith a path in such a module, which will add that path tosys.pathand process any.pthfiles found there.
- 1
- 1
- 1,048,767
- 296
- 4,058
- 3,343
-
so assuming that I don't have (or can't rely on) root access, I would put `sitecustomize.py` in the path pointed to by [`site.USER_SITE`](http://docs.python.org/library/site.html#site.USER_SITE)? – Shep Aug 12 '12 at 13:00
To avoid messing with Python's system installation you could list the paths in .pth files that are in your USER_SITE directory e.g., ~/.local/lib/python2.7/site-packages. You could also put usercustomize.py there and call arbitrary code such as sys.path.insert(0, path), site.addsitedir(path).
- 399,953
- 195
- 994
- 1,670