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?
-
Related: Creating a secondary site-packages directory (and loading packages from .pth files therein)Piotr Dobrogost– Piotr Dobrogost2013-05-02 07:39:12 +00:00Commented May 2, 2013 at 7: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 Comment
sitecustomize.py in the path pointed to by site.USER_SITE?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).