Add to python path mac os x


I thought

import sys

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:

export PATH

To make it work?

8/2/2010 12:54:11 PM

Accepted Answer

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:


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

8/4/2016 4:42:24 AM

Not sure why Matthew's solution didn't work for me (could be that I'm using OSX10.8 or perhaps something to do with macports). But I added the following to the end of the file at ~/.profile

export PYTHONPATH=/path/to/dir:$PYTHONPATH

my directory is now on the pythonpath -

my-macbook:~ aidan$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/path/to/dir', ...  

and I can import modules from that directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow