升级到 macports python 3.5 后安装 python 需要 .so 文件的包时出错

Error installing python packages that require .so files after upgrading to macports python 3.5

我见过类似的问题,但 none 已经给出了我的问题的答案。我最近使用 Mac OS X 上的 Mac 端口升级到 python v3.5。如果有有效的 Mac端口:

sudo port install py35-numpy

但是,如果我尝试使用 pip3 安装 python 需要 .so 文件的包,我会收到错误消息。例如,没有 pystan 的端口。首先,我安装 pip

sudo port install py35-pip

然后我使用 pip 将 pystan 安装到我的 Python 3.5 目录

sudo pip3 install --target=/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/ pystan

Installing collected packages: Cython, numpy, pystan
Successfully installed Cython-0.24 numpy-1.11.1 pystan-2.9.0.0

并不是说它实际上没有安装 Cython 或 numpy,因为它们已经存在。但是当我尝试 运行 pystan:

    Nate$ ipython
Python 3.5.2 (default, Jun 27 2016, 03:10:38) 
Type "copyright", "credits" or "license" for more information.

IPython 5.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import pystan
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-7b354c723dbb> in <module>()
----> 1 import pystan

/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pystan/__init__.py in <module>()
      7 import logging
      8 
----> 9 from pystan.api import stanc, stan
     10 from pystan.misc import read_rdump, stan_rdump
     11 from pystan.model import StanModel

/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pystan/api.py in <module>()
     10 import hashlib
     11 
---> 12 import pystan._api  # stanc wrapper
     13 from pystan._compat import string_types
     14 from pystan.model import StanModel

ImportError: dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pystan/_api.so, 2): Symbol not found: _PyBaseString_Type
  Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pystan/_api.so
  Expected in: flat namespace
 in /opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pystan/_api.so

我尝试通过 pip 安装 numpy 时看到了同样的错误,使用 netCDF4 时也看到了同样的错误。

In [2]: import netCDF4
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-f731da2de255> in <module>()
----> 1 import netCDF4

/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/netCDF4/__init__.py in <module>()
      1 # init for netCDF4. package
      2 # Docstring comes from extension module _netCDF4.
----> 3 from ._netCDF4 import *
      4 # Need explicit imports for names beginning with underscores
      5 from ._netCDF4 import __doc__, __pdoc__

ImportError: dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/netCDF4/_netCDF4.so, 2): Symbol not found: _PyCObject_Type
  Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/netCDF4/_netCDF4.so
  Expected in: flat namespace
 in /opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/netCDF4/_netCDF4.so

.so 文件有什么问题?或者,就此而言,我的电脑?

您确定 pip3 属于 MacPorts 安装的 pip 版本,最好是 pip-3.5?我的猜测是您正在使用来自不同 Python 安装的 pip,因此这将 link C 扩展到错误的 Python 库,并强制安装到包目录中来自 MacPorts 的 Python3.5。

要修复,请改用 pip-3.5(然后您也可以省略 --target 选项)。

编辑:或者执行 sudo port select --set pip pip35 然后只使用 pip.

编辑:我刚刚检查过,确实(当前)MacPorts pip 版本根本不提供 pip3 命令,所以这显然属于不同的 Python 版本。做 which -a pip3 找出哪一个。