Permanently add a directory to PYTHONPATH?

851,406

Solution 1

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.

Solution 2

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

export PYTHONPATH="${PYTHONPATH}:/my/other/path"

Solution 3

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"

Solution 4

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.

Solution 5

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:
851,406
John Howard
Author by

John Howard

Updated on January 27, 2022

Comments

  • John Howard
    John Howard over 2 years

    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?

  • Nathan Ernst
    Nathan Ernst almost 14 years
    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.
  • Alex Martelli
    Alex Martelli almost 14 years
    @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!-).
  • Nathan Ernst
    Nathan Ernst almost 14 years
    unfortunately I'm not an admin on my work PC, so I have to resort to such measures. :(
  • jeremyjjbrown
    jeremyjjbrown almost 12 years
    sys.path.append('/path/to/dir') does not permanently add the entry.
  • Lorcan O'Neill
    Lorcan O'Neill over 11 years
    I've tried this using Python 2.6 and it doesn't seem to work for me
  • firesofmay
    firesofmay about 11 years
    I just symlinked this directory to my own library directory and store all my scripts there. Worked fine.
  • Lorcan O'Neill
    Lorcan O'Neill about 11 years
    Resolved to try it again after finding this topic again and managed to get it working as above this time! Upvoted and contrite apologies :)
  • Mmmh mmh
    Mmmh mmh about 11 years
    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.
  • Joey
    Joey about 11 years
    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 !
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com about 11 years
    in 2.7.4 this python -m site --user-site prints nothing to the screen
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com about 11 years
    ok this worked in 2.7.4: python -c $'import site\nprint site.USER_SITE'
  • appleLover
    appleLover almost 11 years
    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?
  • Gabriel
    Gabriel almost 11 years
    Should the SITEDIR (from above) folder change if using virtualenv? Cause for me it didn't change, but don't know if it's because my main Python install is in the PATH or if it's because the Python in virtualenv still uses that same folder as SITEDIR.
  • b_dev
    b_dev almost 11 years
    remember after you edit ~/.bashrc then run source ~/.bashrc see stackoverflow.com/questions/2518127/…
  • LondonRob
    LondonRob about 10 years
    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.
  • patapouf_ai
    patapouf_ai about 9 years
    I did this, but for some reason, eclipse (the IDE I use) doesn't find the imports I make from files which are in folders I just made a .pth file for. I restarted eclipse but to no avail.
  • patapouf_ai
    patapouf_ai about 9 years
    The python path is correctly added. When I execute print ';'.join(sys.path) the path shows up correctly, my problem is only that eclipse doesn't see the path and gives me an error when I try to import the library.
  • Admin
    Admin almost 8 years
    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.
  • smac89
    smac89 almost 8 years
    Add to .bash_profile if you use login shell, otherwise use .profile
  • aerin
    aerin over 6 years
    This is the answer.
  • Miladiouss
    Miladiouss about 6 years
    Do I need to take an additional step so python would recognize the added path to somelib.pth? If not, any guesses why this might not be working?
  • jxramos
    jxramos about 6 years
    If I understand correctly this will export the variable for Python sessions launched via the terminal only right?
  • Bergrebell
    Bergrebell about 6 years
    @jxramos - yes!
  • Arindam Roychowdhury
    Arindam Roychowdhury almost 6 years
    Anyone wondering how to see path of python, use: which python
  • JayJay123
    JayJay123 almost 6 years
    Windows 7, Python 3.6.5. This is the answer. As auserdude wrote: Create the "example.pth" in Python's Lib/site-packages folder and then add your path like so: C:\somefolder\anotherfolder\targetfolder
  • User
    User over 5 years
    pretty sure you need to reload terminal too
  • Ali80
    Ali80 over 5 years
    excellent answer, also works with python 3.7 on windows 10, my windows 10 path is: "C:\Users\{usrName}\AppData\Local\Programs\Python\Python37-3‌​2\Lib"
  • Anu
    Anu over 5 years
    I did echo PYTHONPATH and nothing showed up! that means this PYTHONPATH haven't been initilised, so I just paste this lineexport PYTHONPATH=$PYTHONPATH:<PATH_TO_TF>/TensorFlow/models/resear‌​ch/object_detection in my ~/.bashrc file and source ~/.bashrc it. Later, when I did ` echo $PYTHONPATH` it gave me :<PATH_TO_TF>/TensorFlow/models/research/object_detection, Any suggestion on if I have done something wrong?
  • boyangeor
    boyangeor over 5 years
    export PYTHONPATH=<PATH_TO_TF>/TensorFlow/models/research/object_de‌​tection, when you are exporting PYTHONPATH for the first time you have to do it without the ${PYTHONPATH}: part
  • Anu
    Anu over 5 years
    Thanks, but whenever I reboot my computer the python path is getting reset to the one I set the 1st i.e :<PATH_TO_TF>/TensorFlow/models/research/object_detection and removes the 2nd export PYTHONPATH=$PYTHONPATH:/TensorFlow/models/research/slim. any suggestions on that, how to fix it?
  • Paul Carlton
    Paul Carlton about 5 years
    Why does this make me think python deployment to production may be a pain the butt.
  • BramAppel
    BramAppel over 4 years
    Can confirm that it works for Python 3.6.7 on Windows 10. However, note that the path is appended to your PYTHONPATH with means that your appended module will not be found in the case of a name collision.
  • amdev
    amdev about 4 years
    actually the export key was the thing that i was missing!!!!, upvoted, thanks alot !
  • juggler
    juggler almost 4 years
    I did this manually (went to arnaud's page..). I had to run my text editor as administrator, and then re-start blender to see the changes.. so I suppose, in principle, if there's a way to improve this answer, or the code, it's to observe the requirement of administrator privaledges, and provide an appropriate solution. that's above my pay-grade (in the sense that more experienced people will have opinions on this.. I leave it to them!) XD
  • juggler
    juggler almost 4 years
    "in Windows, you can do it through the system GUI for the purpose." -this did not work for me. I had to implement andrei deusteanu's solution below..
  • juggler
    juggler almost 4 years
    looks like this is basically equivalent to andrei deusteanu's answer. you will need administrative permission to save to that folder. I'm using blender 2.8, windows 10..
  • Abdelhamed Abdin
    Abdelhamed Abdin over 3 years
    I know the post is old but if you could explain to me what source command is?
  • wpmoradi
    wpmoradi over 3 years
    @abdelhamedabdin The source command reads and executes commands from the file specified as its argument in the current shell environment. It is useful to load functions, variables, and configuration files into shell scripts.
  • Amit Naidu
    Amit Naidu over 3 years
    You can avoid the need for administrator rights (and this entire path searching logic) by writing your .pth file to the directory returned by python -m site --user-site. Also known as python -c "import site; print(site.USER_SITE)". You cannot assume that this user-site directory will exist, but you can create it.
  • Cloud Cho
    Cloud Cho over 3 years
    @y.y Ha ha...Windows Vista came in 2006. i still think mentioning run time hardware environment is helpful in the answer :)
  • y.y
    y.y over 3 years
    you are right tho, also it made me smile :D have a nice day Sir :)
  • DougR
    DougR over 3 years
    Brilliant, this code helped me create something for my requirements. Cheers!!! stackoverflow.com/a/66372128/2007153
  • geominded
    geominded about 2 years
    How should the export looks if I'm adding the dir from a setup.sh script? e.g. export 'export PYTHONPATH=$PYTHONPATH:$(pwd)/src' >> ~/.bashrc just gets printed to the file. How can I evaluate the $(pwd) before exporting it to the ~/.bashrc ?