Permanently add a directory to PYTHONPATH?
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
- On Windows, with Python 2.7 go to the Python setup folder.
- Open Lib/site-packages.
- Add an example.pth empty file to this folder.
- 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:
- Open up Terminal
- Type
open .bash_profile
- In the text file that pops up, add this line at the end:
export PYTHONPATH=$PYTHONPATH:foo/bar
- Save the file, restart the Terminal, and you're done
John Howard
Updated on January 27, 2022Comments
-
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 toPYTHONPATH
? -
Nathan Ernst almost 14 yearsErrata: 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 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 almost 14 yearsunfortunately I'm not an admin on my work PC, so I have to resort to such measures. :(
-
jeremyjjbrown almost 12 yearssys.path.append('/path/to/dir') does not permanently add the entry.
-
Lorcan O'Neill over 11 yearsI've tried this using Python 2.6 and it doesn't seem to work for me
-
firesofmay about 11 yearsI just symlinked this directory to my own library directory and store all my scripts there. Worked fine.
-
Lorcan O'Neill about 11 yearsResolved 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 about 11 yearsThis 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 about 11 yearsThis 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 about 11 yearsin
2.7.4
thispython -m site --user-site
prints nothing to the screen -
Ciro Santilli OurBigBook.com about 11 yearsok this worked in 2.7.4:
python -c $'import site\nprint site.USER_SITE'
-
appleLover almost 11 yearsthis 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 almost 11 yearsShould 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 almost 11 yearsremember after you edit
~/.bashrc
then runsource ~/.bashrc
see stackoverflow.com/questions/2518127/… -
LondonRob about 10 yearsI 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 about 9 yearsI 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 about 9 yearsThe 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 almost 8 yearsIMO, 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 almost 8 yearsAdd to
.bash_profile
if you use login shell, otherwise use.profile
-
aerin over 6 yearsThis is the answer.
-
Miladiouss about 6 yearsDo 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 about 6 yearsIf I understand correctly this will export the variable for Python sessions launched via the terminal only right?
-
Bergrebell about 6 years@jxramos - yes!
-
Arindam Roychowdhury almost 6 yearsAnyone wondering how to see path of python, use: which python
-
JayJay123 almost 6 yearsWindows 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 over 5 yearspretty sure you need to reload terminal too
-
Ali80 over 5 yearsexcellent answer, also works with python 3.7 on windows 10, my windows 10 path is: "C:\Users\{usrName}\AppData\Local\Programs\Python\Python37-32\Lib"
-
Anu over 5 yearsI 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/research/object_detection
in my~/.bashrc
file andsource ~/.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 over 5 years
export PYTHONPATH=<PATH_TO_TF>/TensorFlow/models/research/object_detection
, when you are exporting PYTHONPATH for the first time you have to do it without the${PYTHONPATH}:
part -
Anu over 5 yearsThanks, 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 2ndexport PYTHONPATH=$PYTHONPATH:/TensorFlow/models/research/slim
. any suggestions on that, how to fix it? -
Paul Carlton about 5 yearsWhy does this make me think python deployment to production may be a pain the butt.
-
BramAppel over 4 yearsCan 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 about 4 yearsactually the
export
key was the thing that i was missing!!!!, upvoted, thanks alot ! -
juggler almost 4 yearsI 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 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 almost 4 yearslooks 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 over 3 yearsI know the post is old but if you could explain to me what source command is?
-
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 over 3 yearsYou 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 aspython -c "import site; print(site.USER_SITE)"
. You cannot assume that this user-site directory will exist, but you can create it. -
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 over 3 yearsyou are right tho, also it made me smile :D have a nice day Sir :)
-
DougR over 3 yearsBrilliant, this code helped me create something for my requirements. Cheers!!! stackoverflow.com/a/66372128/2007153
-
geominded about 2 yearsHow 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 ?