How to permanently append a directory to PYTHONPATH?

12,753

Solution 1

If you put the second method in your shell's init file, you should be fine. (for example, ${HOME}/.bashrc)

Solution 2

PYTHONPATH is a system wide variable, so it has to be set up in a more permanent way (basically, that export PYTHONPATH=$PYTHONPATH:foo/bar needs to be executed automatically by whatever shell is then executing python) - os specific instructions are below:

Windows: http://docs.python.org/using/windows.html#excursus-setting-environment-variables

Mac/Unix: http://users-cs.au.dk/chili/PBI/pythonpath.html

Solution 3

I suggest to use export PYTHONPATH=foo/bar:$PYTHONPATH if you prefer your custom library to be found before the default if they have the same name.

Share:
12,753
entrepaul
Author by

entrepaul

Updated on June 04, 2022

Comments

  • entrepaul
    entrepaul almost 2 years

    I attempted the two commonly mentioned methods below, and they have not worked - hence this [seemingly redundant] question.

    import sys
    sys.path.append('foo/bar')
    

    AND

    export PYTHONPATH=$PYTHONPATH:foo/bar
    

    The first one terminates the append once interpreter is exited. The second terminates when terminal is closed (despite the fact that people seem to have no problem with permanently appending via the second method).

    What am I missing here and how do I resolve this issue?

    Thank you.