pythonmacosadditionpythonpath

Add to python path mac os x


I thought

import sys
sys.path.append("/home/me/mydir")

is appending a dir to my pythonpath

if I print sys.path my dir is in there.

Then I open a new command and it is not there anymore.

But somehow Python cant import modules I saved in that dir.

What Am I doing wrong?

I read .profile or .bash_profile will do the trick.

Do I have to add:

PATH="/Me//Documents/mydir:$PYTHONPATH"
export PATH

To make it work?


Solution

  • Modifications to sys.path only apply for the life of that Python interpreter. If you want to do it permanently you need to modify the PYTHONPATH environment variable:

    PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH"
    export PYTHONPATH
    

    Note that PATH is the system path for executables, which is completely separate.

    **You can write the above in ~/.bash_profile and the source it using source ~/.bash_profile