IDLE和Python在Mac OS X中有不同的路径

时间:2009-09-19 06:34:46

标签: python django

我正在运行Mac OS X 10.5.8。我从网站上安装了Python 2.6。它在我的应用程序目录中。我编辑了我的.bash_profile:

# 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

当我从命令提示符运行python时,我可以得到以下内容:

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)

检查路径

>>> 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']
>>> 

当我处于空闲状态时,我会得到不同的体验。

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']

我不知道发生了什么。我从一台笔记本电脑转移到另一台笔记本电脑并完成了整个TimeCapsule的事情,所以我知道因为这个而存在一些冲突。 IDLE从哪里获得PATH?为什么我不能导入Django?

2 个答案:

答案 0 :(得分:2)

与所有OS X应用程序包一样,如果通过双击启动IDLE.app,则不涉及shell,因此不会调用bash_profile或其他shell初始化文件。有一种方法可以通过使用特殊的property list file~/.MacOSX/environment.plist)来设置用户会话环境变量,但它确实有点像kludge而不推荐。

幸运的是,有一个更简单的解决方案:在OS X上,还可以从终端窗口中的shell命令行调用IDLE。通过这种方式,它将像您期望的那样从该shell继承导出的环境变量。如下所示:

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

在2.6.2之前的OS X上,IDLE存在各种不一致和问题,具体取决于它的调用方式,因此我建议在OS X上使用比python.org 2.6.2或3.1版本更旧的版本。

编辑:我从open(1)手册页看到,自10.4以来,通过open启动的应用程序也继承了环境变量,因此也可以从命令行运行。如果你想避免打开一个终端窗口,可以很容易地使用AppleScript或Automator创建一个简单的启动器应用程序(甚至Python和py2app!)。在这种情况下,请使用open命令命令,以便启动器应用程序不会出现。例如,在Automator中,选择Run Shell Script操作并添加:

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

将其另存为文件格式Application(在10.5中),您应该有一种可点击的方式来启动定制的IDLE。

答案 1 :(得分:0)

似乎是一个常见问题:

http://www.google.com/search?q=python+idle+pythonpath

不幸的是,没有答案。唯一的建议似乎是编辑idle可执行文件并添加一些sys.path.insert(...)行。