安装 MacPorts 后,获取 python 的正确 PATH

安装 MacPorts 后,获取 python 的正确 PATH

我无法导入刚刚使用 MacPorts 安装的一些 Python 库(PIL、psycopg2)。我浏览了这些论坛,并尝试调整我的PATH变量以~/.bash_profile修复此问题,但没有成功。

我将 PIL 和 psycopg2 的位置添加到PATH
我知道 Terminal 使用的是 中的 python 版本,/usr/local/bin而不是 MacPorts 在 处安装的版本/opt/local/bin

当我在终端中使用 python 时,我是否需要使用 MacPorts 版本的 Python 来确保 PIL 和 psycopg2 位于 sys.path 上?我应该切换到 MacPorts 版本的 Python 吗,还是会导致更多问题?

如果有帮助的话,下面还有更多事实:

PIl 和 psycopg2 安装在/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages

which python返回/usr/bin/python

$ echo $PATH返回(为了方便阅读,我将每条路径分开):

:/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/
:/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
:/opt/local/bin
:/opt/local/sbin
:/usr/local/git/bin
:/usr/bin
:/bin
:/usr/sbin
:/sbin
:/usr/local/bin
:/usr/local/git/bin
:/usr/X11/bin
:/opt/local/bin

在 python 中,>>> sys.path返回:

/Library/Frameworks/SQLite3.framework/Versions/3/Python
/Library/Python/2.6/site-packages/numpy-override
/Library/Frameworks/GDAL.framework/Versions/1.7/Python/site-packages
/Library/Frameworks/cairo.framework/Versions/1/Python
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages
 /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload
/Library/Python/2.6/site-packages
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode

如果以上任何内容看起来愚蠢或构思不当,我欢迎任何批评和评论。我对这些都很陌生。谢谢!

在 MacBook Pro 上运行 OSX 10.6.5,从终端调用 python 2.6.1

答案1

安装 python 后使用‘notes’命令(将 python26 更改为您安装的任何版本):

$ port notes python26

您会看到安装后建议有一些变化:

python26 has the following notes:
  To fully complete your installation and make python 2.6 the default,  please
  run:
    sudo port install python_select
    sudo port select python python26

您应该安装python_select并选择python26作为默认 Python。然后,如果您确实使用 MacPorts(即类似port install py26-psycopg2 py26-pil)安装了 PIL 和 pyscopg2,那么您不必指定任何事物在 中PYTHONPATH。您也不需要将/opt/local/Library/Frameworks/…目录添加到 中PATH。完成此操作后,which python应返回/opt/local/bin/python。MacPorts 在 中安装的 Python 端口/opt/local/Library/Frameworks/Python.framework已在sys.pathMacPorts Python 中。您之前没有找到它们,因为您使用的是 System Python(即/usr/bin/python),它不了解 MacPorts Python 包。

答案2

思考我找到了问题的答案。我编辑了 ~/.bash_profile 文件以包含

export PYTHONPATH=

然后我添加了所有现有路径sys.path

export PYTHONPATH=/Library/Frameworks/SQLite3.framework/Versions/3/Python:/Library/Python/2.6/site-packages/numpy-override:/Library/Frameworks/GDAL.framework/Versions/1.7/Python/site-packages:/Library/Frameworks/cairo.framework/Versions/1/Python:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages:/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload:/Library/Python/2.6/site-packages:/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC:/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode:/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/:/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages:

然后我将路径附加到 MacPorts 安装目录:

/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/:/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages:

现在,当我打印 sys.path 时,将包含 MacPorts 安装目录的路径:

/Library/Frameworks/SQLite3.framework/Versions/3/Python
/Library/Python/2.6/site-packages/numpy-override
/Library/Frameworks/GDAL.framework/Versions/1.7/Python/site-packages
/Library/Frameworks/cairo.framework/Versions/1/Python
/Library/Frameworks/SQLite3.framework/Versions/3/Python
/Library/Python/2.6/site-packages/numpy-override
/Library/Frameworks/GDAL.framework/Versions/1.7/Python/site-packages
/Library/Frameworks/cairo.framework/Versions/1/Python
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload
/Library/Python/2.6/site-packages
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages

现在

>>> import PIL

>>> import psycopg2

工作得很好。

相关内容