RuntimeError:针对 API 版本 0xc 编译的模块,但这个版本的 numpy 是 0xb

Posted

技术标签:

【中文标题】RuntimeError:针对 API 版本 0xc 编译的模块,但这个版本的 numpy 是 0xb【英文标题】:RuntimeError: module compiled against API version 0xc but this version of numpy is 0xb 【发布时间】:2018-06-11 18:41:56 【问题描述】:

我正在尝试学习机器学习,但我无法解决以下错误。

[我的环境]

Mac High Sierra 10.13.2 Python3.4.5 Numpy1.13.3

[命令]

$ python3 -c "import jupyter, matplotlib, numpy, pandas, scipy, sklearn"

[错误]

RuntimeError: module compiled against API version 0xc but this version of numpy is 0xb
Traceback (most recent call last):
  File "/Users/uekyo/ml/env/lib/python3.4/site-packages/pandas/__init__.py", line 26, in <module>
    from pandas._libs import (hashtable as _hashtable,
  File "/Users/uekyo/ml/env/lib/python3.4/site-packages/pandas/_libs/__init__.py", line 4, in <module>
    from .tslib import iNaT, NaT, Timestamp, Timedelta, OutOfBoundsDatetime
  File "pandas/_libs/tslib.pyx", line 1, in init pandas._libs.tslib
ImportError: numpy.core.multiarray failed to import

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/uekyo/ml/env/lib/python3.4/site-packages/pandas/__init__.py", line 35, in <module>
    "the C extensions first.".format(module))
ImportError: C extension: numpy.core.multiarray failed to import not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.

[我尝试过的事情]

我理解 numpy 是错误的。所以我尝试了下面的事情,但错误仍然出现。

~1~

sudo mv /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy_old

~2~

pip3 uninstall numpy
pip3 install numpy  -I

谢谢。

【问题讨论】:

1) 移动 NumPy 的 Python2 安装不会做任何事情,因为您的命令都是 Python3。 2) 导入pandas时出现错误,请尝试重新安装。 感谢您的建议,但又失败了。错误是同一个。 我发现这个命令没问题。 python3 -c "import jupyter, matplotlib, numpy, scipy, sklearn jupyter==1.0.0 matplotlib==2.0.2 numexpr==2.6.3 numpy==1.13.1 pandas==0.20.3 Pillow==4.2.1 protobuf==3.4.0 psutil==5.3.1 scikit-learn==0.19.0 scipy==0.19.1 sympy==1.1.1 tensorflow==1.3.0 谢谢你。您的 pandas 建议为我提供了解决方案。 【参考方案1】:

好像是因为pandas版本高于0.21.0numpy 1.13.3版被TensorFlow拉出来的效果不好。

这是我最后修复它的方法:

# ipython
Python 2.7.6 (default, Nov 23 2017, 15:49:48)
Type "copyright", "credits" or "license" for more information.

IPython 4.2.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 tensorflow as tf
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
RuntimeError: module compiled against API version 0xc but this version of numpy is 0xb
    
In [2]: !pip install pandas==0.21.0
Collecting pandas==0.21.0
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request
has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server
to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve
 this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLC
ontext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to f
ail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest
/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading pandas-0.21.0-cp27-cp27mu-manylinux1_x86_64.whl (24.3MB)
    100% |################################| 24.3MB 52kB/s
Requirement already satisfied: pytz>=2011k in /usr/local/lib/python2.7/dist-packages (from pandas==0.21.0)
Requirement already satisfied: numpy>=1.9.0 in /usr/local/lib/python2.7/dist-packages (from pandas==0.21.0)
Requirement already satisfied: python-dateutil in /usr/local/lib/python2.7/dist-packages (from pandas==0.21.0)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python2.7/dist-packages (from python-dateutil->pandas==0.21.0)
Installing collected packages: pandas
  Found existing installation: pandas 0.21.1
    Uninstalling pandas-0.21.1:
      Successfully uninstalled pandas-0.21.1
Successfully installed pandas-0.21.0

In [3]: del tf

In [4]: import tensorflow as tf

【讨论】:

我还为Python3 测试了这个,似乎已经解决了我的问题。 感谢您的详细建议。是的。你说得对。我可以解决它。 我的荣幸@uekyo 谢谢@abolfazlshahbazi。就我而言,我不得不将 scipy 重新安装到 scipy=1.1.0 并且 numpy 是 numpy==1.3.3【参考方案2】:

我有同样的问题。如果您使用的是 anaconda,只需键入

conda 更新张量流

这应该可以解决问题。

【讨论】:

【参考方案3】:

很可能是由于 numpy 版本太低;这个解决了我的问题:

pip3 install "numpy == 1.15.0" --user 

conda install 无法解决,因为目前它只有 numpy 版本 1.13.1,但可能是由于我选择的镜像站点不是最新的

【讨论】:

【参考方案4】:

基于当前安装的 tensorflow 或 sklearn 包,可能存在一些不兼容。你应该尝试不同的 numpy 版本。顺便说一句,就我而言,从 1.18 降级到 1.15 解决了这个问题。

pip install "numpy == 1.15.0"

【讨论】:

以上是关于RuntimeError:针对 API 版本 0xc 编译的模块,但这个版本的 numpy 是 0xb的主要内容,如果未能解决你的问题,请参考以下文章

RuntimeError:针对 API 版本 a 编译的模块,但这个版本的 numpy 是 9

Opencv / numpy问题:“针对API版本X编译的模块,但这个版本的numpy是Y”

RuntimeError: module compiled against API version 0xa but this version of numpy is 0x9 / ImportError

RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa

解决 RuntimeError: module compiled against API version 0xf but this version of numpy is 0xd

解决 RuntimeError: module compiled against API version 0xf but this version of numpy is 0xd