Python – IDLE and Python have different path in Mac OS X

djangopython

I am running Mac OS X 10.5.8. I have installed Python 2.6 from the site. It's in my application directory. I have edited my .bash_profile to have:

# Setting PATH for MacPython 2.6
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:${PATH}"
export PATH
export PATH=/usr/local/mysql/bin:/Library/Frameworks/Python.framework/Versions/2.6/bin:/Library/Frameworks/Python.framework/Versions/2.6/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
export PYTHONPATH=/Users/blwatson/pythonpath:/Users/blwatson/pythonpath/django/bin:$PYTHONPATH

when I run python from the command prompt, I can get the following:

Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) 
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 0, 4, 'alpha', 0)

checking PATH

>>> import sys
>>> sys.path
['', '/Users/blwatson/pythonpath', '/Users/blwatson/pythonpath/django/bin', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/wx-2.8-mac-unicode']
>>> 

When I am in IDLE, I get a different experience.

Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) 
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************

IDLE 2.6.2      
>>> import django

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import django
ImportError: No module named django
>>> import sys
>>> print sys.path
['/Users/blwatson/Documents', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/wx-2.8-mac-unicode']

I have no idea what's going on. I moved from one laptop to another and did the whole TimeCapsule thing, so I know that there's some conflict because of that. Where is IDLE getting the PATH from? Why can't I import Django?

Best Solution

Like all OS X application bundles, if you launch IDLE.app by double-clicking, a shell is not involved and thus .bash_profile or other shell initialization files are not invoked. There is a way to set user session environment variables through the use of a special property list file (~/.MacOSX/environment.plist) but it really is a bit of a kludge and not recommended.

Fortunately, there is a simpler solution: on OS X, it is also possible to invoke IDLE from a shell command line in a terminal window. In this way, it will inherit exported environment variables from that shell as you would expect. So something like:

$ export PYTHONPATH= ...
$ /usr/local/bin/idle2.6

There were various inconsistencies and problems with IDLE on OS X prior to 2.6.2 depending on how it was invoked so I recommend using nothing older than the python.org 2.6.2 or 3.1 versions on OS X.

EDIT: I see from the open(1) man page that, since 10.4, applications launched via open also inherit environment variables so that would work from the command line as well. If you want to avoid opening a terminal window, it is easy to create a simple launcher app using AppleScript or Automator (or even Python with py2app!). In this case, use the open command command so that the launcher app does not sit around. For example, in Automator, choose the Run Shell Script action and add:

export PYTHONPATH= ...
open -a "/Applications/Python 2.6/IDLE.app"

Save it as File Format Application (in 10.5) and you should have a clickable way to launch a tailored IDLE.