Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Whenever I use sys.path.append, the new directory will be added. However, once I close python, the list will revert to the previous (default?) values. How do I permanently add a directory to PYTHONPATH?

share|improve this question

8 Answers 8

up vote 47 down vote accepted

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.

share|improve this answer
4  
Errata: separator on windows would a semicolon. If you need to override system paths on windows, setting via the GUI as a user environment variable may not be sufficient, as user variables are appended to system variables. In these cases, you'll need to resort to a startup script that makes the necessary adjustments. –  Nathan Ernst Aug 4 '10 at 3:18
    
@Nathan, tx for the reminder on semicolon, but (if you're an admin of course) you can set system env.vars on windows (plus, the OP is not asking how to override the path, just how to append to it, so, a user env.var will also be fine for that!-). –  Alex Martelli Aug 4 '10 at 4:16
    
unfortunately I'm not an admin on my work PC, so I have to resort to such measures. :( –  Nathan Ernst Aug 4 '10 at 22:13

If you're using bash (on a Mac or GNU/Linux distro), add this to your ~/.bashrc

export PYTHONPATH="${PYTHONPATH}:/my/other/path"
share|improve this answer
5  
This worked perfectly for me, but make sure the directory you point to has at the topmost init.py file in your directory structure. This wasn't perfectly clear for me at first. For example, I tried export PYTHONPATH=$PYTHONPATH:/Users/joey/repos but it did not work because my repos directory did not have _init_.py. Going down one directory further: /Users/joey/repos/specificRepo did the trick. Now python can traverse any downward directory connected to the specificRepo directory that contains a init.py ! –  Qiao Yi Mar 14 '13 at 14:29
    
this worked for me but could you explain where this PYTHONPATH variable is located? and how does "export PYTHONPATH" know to locate that exact file? –  appleLover Jun 7 '13 at 17:23
5  
remember after you edit ~/.bashrc then run source ~/.bashrc see stackoverflow.com/questions/2518127/… –  indiehacker Aug 13 '13 at 16:02
1  
I think it's a bad idea to put sudo su at the start of your .bashrc file. This post agrees with me. On my system, at least, it's not even necessary. –  LondonRob Apr 14 '14 at 10:00
    
It did not work for me :( Using Ubuntu 14.04 –  moldovean May 7 '14 at 11:23

You could add the path via your pythonrc file, which defaults to ~/.pythonrc on linux. ie.

import sys
sys.path.append('/path/to/dir')

You could also set the PYTHONPATH environment variable, in a global rc file, such ~/.profile on mac or linux, or via Control Panel -> System -> Advanced tab -> Environment Variables on windows.

share|improve this answer
15  
sys.path.append('/path/to/dir') does not permanently add the entry. –  jeremyjjbrown Jul 28 '12 at 22:26

Just to add on awesomo's answer, you can also add that line into your ~/.bash_profile or ~/.profile

share|improve this answer

Instead of manipulating PYTHONPATH you can also create a path configuration file. First find out in which directory Python searches for this information:

python -m site --user-site

For some reason this doesn't seem to work in Python 2.7. There you can use:

python -c 'import site; site._script()' --user-site

Then create a .pth file in that directory containing the path you want to add (create the directory if it doesn't exist).

For example:

# find directory
SITEDIR=$(python -m site --user-site)

# create if it doesn't exist
mkdir -p "$SITEDIR"

# create new .pth file with our path
echo "$HOME/foo/bar" > "$SITEDIR/somelib.pth"
share|improve this answer
    
I've tried this using Python 2.6 and it doesn't seem to work for me –  Lorcan O'Neill Feb 26 '13 at 18:02
1  
I just symlinked this directory to my own library directory and store all my scripts there. Worked fine. –  mayjune Mar 5 '13 at 6:08
    
Resolved to try it again after finding this topic again and managed to get it working as above this time! Upvoted and contrite apologies :) –  Lorcan O'Neill Mar 7 '13 at 11:20
    
This works just perfectly, I was on the right track but the python -m site --user-site and (create the directory if it doesn't exist) parts were what I was missing to get it working. –  Aurélien Ooms Mar 9 '13 at 21:33
    
in 2.7.4 this python -m site --user-site prints nothing to the screen –  Ciro Santilli 六四事件 法轮功 May 6 '13 at 15:07

In case anyone is still confused - if you are on a Mac, do the following:

  1. Open up Terminal
  2. Type open .bash_profile
  3. In the text file that pops up, add this line at the end: export PYTHONPATH=$PYTHONPATH:foo/bar
  4. Save the file, restart the Terminal, and you're done
share|improve this answer

On linux you can create a symbolic link from your package to a directory of the PYTHONPATH without having to deal with the environment variables. Something like:

ln -s /your/path /usr/lib/pymodules/python2.7/
share|improve this answer

For me it worked when I changed the .bash_profile file. Just changing .bashrc file worked only till I restarted the shell.

For python 2.7 it should look like:

export PYTHONPATH="$PYTHONPATH:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python"

at the end of the .bash_profile file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.