安装MacPorts后,让PATH适合python


1

我无法导入我刚刚安装MacPorts的一些python库(PIL,psycopg2)。我浏览了这些论坛,并试图调整我的 PATH 变量 ~/.bash_profile 为了解决这个问题但它不起作用。

我添加了PIL和psycopg2的位置 PATH
我知道终端正在使用python的一个版本 /usr/local/bin 而不是MacPorts安装的那个 /opt/local/bin

我是否需要使用MacPorts版本的Python以确保在终端中使用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

Answers:


5

安装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安装了PIL和pyscopg2(例如 port install py26-psycopg2 py26-pil ),那么你不应该指定 什么 PYTHONPATH。您也不需要添加 /opt/local/Library/Frameworks/… 你的目录 PATH。这样做之后 which python 应该回来 /opt/local/bin/python。 MacPorts安装的Python端口 /opt/local/Library/Frameworks/Python.framework 已经在 sys.path 对于MacPorts Python。您之前没有找到它们,因为您使用的是System Python(即 /usr/bin/python ),它不了解MacPorts Python包。


谢谢fideli。刚试了一下。这非常简单,完全奏效。这是正确的方法。
BenjaminGolder

为什么macports要求你选择这个python?为什么它不是自己选择的(至少在我的机器上,我在macports下第一次安装了python;它不应该自动选择它吗?)
kalaracey

当有明显的冲突时,@ kalaracey MacPorts试图成为非侵入性的,即MacPorts vs System Python。如果你想使用 python26 通过MacPorts安装,您必须使用它 python-select。请注意,使用Python的完整路径 /opt/local/bin/python2.6 一直都很有效。
fideli

1
这些天看起来像: $ port notes python27 sudo port select --set python python27
smci

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

工作得很好。


不幸的是,这并没有真正解决同一个库的多个版本的问题。如果在两个目录中有一个具有相同名称的库,并且两者都在PATH中,那么如何知道将使用哪个?
BenjaminGolder

找到一本关于UNIX和shell编程的书,它说当给出一个命令时,shell将根据它们列出的顺序搜索PATH中的目录。换句话说,将首先搜索键入echo $ PATH时列出的第一个目录。我认为PYTHONPATH也是如此。
BenjaminGolder
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.