MacPorts 安裝後取得正確的 python 路徑

MacPorts 安裝後取得正確的 python 路徑

我無法導入剛剛隨 MacPorts 安裝的一些 python 函式庫(PIL、psycopg2)。我瀏覽了這些論壇,並嘗試調整我的PATH變數以~/.bash_profile解決此問題,但它不起作用。

我將 PIL 和 psycopg2 的位置加到PATH.
我知道終端機使用的是 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 安裝了 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.pathfor MacPorts 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

工作得很好。

相關內容