2

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?

Shep
  • 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 Answers2

4

You have two options:

  • List additional paths in a .pth file in one of the standard locations (usually your site-packages location). See How to add a Python import path using a .pth file as well.

  • Add additional paths to sys.path in sitecustomize or usercustomize modules (detailed in the site module documentation). Your sitecustomize or usercustomize could look something like:

    import sys
    sys.path[0:0] = [
        '/foo/bar',
        '/spam/eggs',
    ]
    

    where the two extra entries would be inserted into sys.path at the front.

    You can also call site.addsitedir with a path in such a module, which will add that path to sys.path and process any .pth files found there.

Community
  • 1
  • 1
Martijn Pieters
  • 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
2

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).

jfs
  • 399,953
  • 195
  • 994
  • 1,670