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
How to Check If an Object Is a List or Tuple (But Not String)
Mixing Cdef and Regular Python Attributes in Cdef Class
Attributeerror: 'Client' Object Has No Attribute 'Send_Message' (Discord Bot)
Remove Non-Ascii Characters from Pandas Column
How to Read Datetime Back from SQLite as a Datetime Instead of String in Python
How to Convert Escaped Characters
Import Module Works in Terminal But Not in Idle
How to Sort a Pandas Dataframe by Index
How to Normalize JSON Correctly by Python Pandas
How to Create an Object and Add Attributes to It
How to Clone a Python Generator Object
How to Create a Decorator That Can Be Used Either with or Without Parameters
Replace Invalid Values with None in Pandas Dataframe
Generating Discrete Random Variables with Specified Weights Using Scipy or Numpy