Permanently Adding a File Path to Sys.Path in Python

Permanently add a directory to PYTHONPATH?

You need to add your new directory to the environment variable PYTHONPATH, separated by a colon from previous contents thereof. In any form of Unix, you can do that in a startup script appropriate to whatever shell you're using (.profile or whatever, depending on your favorite shell) with a command which, again, depends on the shell in question; in Windows, you can do it through the system GUI for the purpose.

superuser.com may be a better place to ask further, i.e. for more details if you need specifics about how to enrich an environment variable in your chosen platform and shell, since it's not really a programming question per se.

Adding folder to Python's path permanently

The PYTHONPATH environment variable will do it.

Permanently adding a file path to sys.path in Python

There are a few ways. One of the simplest is to create a my-paths.pth file (as described here). This is just a file with the extension .pth that you put into your system site-packages directory. On each line of the file you put one directory name, so you can put a line in there with /path/to/the/ and it will add that directory to the path.

You could also use the PYTHONPATH environment variable, which is like the system PATH variable but contains directories that will be added to sys.path. See the documentation.

Note that no matter what you do, sys.path contains directories not files. You can't "add a file to sys.path". You always add its directory and then you can import the file.

How do I add a Python import path permanently?

From man python

   ~/.pythonrc.py
User-specific initialization file loaded by the user module; not used by default or by most applications.

ENVIRONMENT VARIABLES

PYTHONPATH
Augments the default search path for module files. The format is the same as the shell's $PATH: one or more directory pathnames
separated by colons. Non-existent directories are silently ignored. The default search path is installation dependent, but gen-
erally begins with ${prefix}/lib/python<version> (see PYTHONHOME above). The default search path is always appended to $PYTHON-
PATH. If a script argument is given, the directory containing the script is inserted in the path in front of $PYTHONPATH. The
search path can be manipulated from within a Python program as the variable sys.path .

adding directory to sys.path /PYTHONPATH

This is working as documented. Any paths specified in PYTHONPATH are documented as normally coming after the working directory but before the standard interpreter-supplied paths. sys.path.append() appends to the existing path. See here and here. If you want a particular directory to come first, simply insert it at the head of sys.path:

import sys
sys.path.insert(0,'/path/to/mod_directory')

That said, there are usually better ways to manage imports than either using PYTHONPATH or manipulating sys.path directly. See, for example, the answers to this question.



Related Topics



Leave a reply



Submit