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?

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.