Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

10 Answers 10

up vote 74 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. 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
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
This worked perfectly for me, but make sure the directory you point to has at the topmost 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 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 ! – 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
remember after you edit ~/.bashrc then run source ~/.bashrc see… – b_dev Aug 13 '13 at 16:02
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

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
I just symlinked this directory to my own library directory and store all my scripts there. Worked fine. – firesofmay 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 烏坎事件2016六四事件 法轮功 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

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

import sys

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
sys.path.append('/path/to/dir') does not permanently add the entry. – jeremyjjbrown Jul 28 '12 at 22:26

This works on Windows

  1. On Windows, with Python 2.7 go to the Python setup folder.
  2. Open Lib/site-packages.
  3. Add an example.pth empty file to this folder.
  4. Add the required path to the file, one per each line.

Then you'll be able to see all modules within those paths from your scripts.

share|improve this answer
IMO, this is the best solution, since it does not depend on choice of shell or working environment. Works on linux as well, where the default python path is "/usr/local/lib/pythonX.Y/site-packages". If that doesn't work, try putting your .pth file to dist-packages directory instead. – matus Jul 28 at 12:49

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

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

share|improve this answer
Add to .bash_profile if you use login shell, otherwise use .profile – smac89 Aug 11 at 18:30

To give a bit more explanation, Python will automatically construct its search paths (as has mentioned above and here) using the script (typically located in sys.prefix + lib/python/site-packages as well as lib/site-python). One can obtain the value of sys.prefix:

python -c 'import sys; print(sys.prefix)'

The script then adds a number of directories, dependent upon the platform, such as /usr/{lib,share}/python/dist-packages, /usr/local/lib/python/dist-packages to the search path and also searches these paths for <package>.pth config files which contain specific additional search paths. For example easy-install maintains its collection of installed packages which are added to a system specific file e.g on Ubuntu it's /usr/local/lib/python2.7/dist-packages/easy-install.pth. On a typical system there are a bunch of these .pth files around which can explain some unexpected paths in sys.path:

python -c 'import sys; print(sys.path)'

So one can create a .pth file and put in any of these directories (including the sitedir as mentioned above). This seems to be the way most packages get added to the sys.path as opposed to using the PYTHONPATH.

share|improve this answer

Your Answer


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.