views:

229

answers:

3

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?

Using Windows.

+2  A: 

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

export PYTHONPATH=$PYTHONPATH:/my/other/path
awesomo
+3  A: 

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.

Alex Martelli
Thanks, this worked.
Zonda333
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
@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
@Alex, unfortunately I'm not an admin on my work PC, so I have to resort to such measures. :(
Nathan Ernst
A: 

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.

Blue Peppers