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?
This question is related to
python
macos
add
pythonpath
Mathew's answer works for the terminal python shell, but it didn't work for IDLE shell in my case because many versions of python existed before I replaced them all with Python2.7.7. How I solved the problem with IDLE.
cd /Applications/Python\ 2.7/IDLE.app/Contents/Resources/
sudo nano idlemain.py
, enter password if required.os.chdir(os.path.expanduser('~/Documents'))
this line, I added sys.path.append("/Users/admin/Downloads....")
NOTE: replace contents of the quotes with the directory where python module to be addedNot 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.
Setting the $PYTHONPATH environment variable does not seem to affect the Spyder IDE's iPython terminals on a Mac. However, Spyder's application menu contains a "PYTHONPATH manager." Adding my path here solved my problem. The "PYTHONPATH manager" is also persistent across application restarts.
This is specific to a Mac, because setting the PYTHONPATH environment variable on my Windows PC gives the expected behavior (modules are found) without using the PYTHONPATH manager in Spyder.
Source: Stackoverflow.com