cython setup.py导入numpy失败

时间:2019-07-03 12:38:02

标签: numpy cython importerror

在我的setup.py文件中导入numpy失败,并显示错误消息:

File "setup.py", line 17, in <module>
    import numpy
  File "C:\Users\Anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
    from . import _distributor_init
  File "C:\Users\Anaconda3\lib\site-packages\numpy\_distributor_init.py", line 34, in <module>
    from . import _mklinit
ImportError: DLL load failed: The specified module could not be found.

似乎是特定于numpy的。例如,导入os可以正常工作。或者,当我尝试导入pandas时,错误提示ImportError: Missing required dependencies ['numpy']

在这里设置文件

try:
    from setuptools import setup
    from setuptools import Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension

from Cython.Distutils import build_ext
import numpy 

ext_modules = [Extension('helloworld',
                         ['helloworld.pyx'])]

setup(cmdclass = {'build_ext': build_ext}, ext_modules = ext_modules)

.pyx文件

print('Hello World')

(在.py脚本中导入numpy可以正常工作。) 我在Windows 10上使用最新的Anaconda发行版3.7,numpy版本1.16.2和cython版本0.29.6。为了编译C文件,我使用了Visual Studio Community 2019中包含的编译器。

我想这没什么大不了的,但是我在网上搜索了一天,却没有成功。任何帮助深表感谢。

编辑的其他信息:我安装了旧版本的Anaconda发行版(Anaconda3-5.3.1-Windows-x86_64.exe),但遇到相同的问题

C:\Users\Documents\cython>python setup.py build_ext --inplace
Traceback (most recent call last):
  File "C:\Users\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 16, in <module>
    from . import multiarray
ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "setup.py", line 17, in <module>
    import numpy
  File "C:\Users\Anaconda3\lib\site-packages\numpy\__init__.py", line 142, in <module>
    from . import add_newdocs
  File "C:\Users\Anaconda3\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "C:\Users\Anaconda3\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
    from .type_check import *
  File "C:\Users\Anaconda3\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "C:\Users\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 26, in <module>
    raise ImportError(msg)
ImportError:
Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.

EDIT2 解决方法:我导入numpy,因为最终我想在.pyx文件中使用其功能。为此,通常会在include_dirs=[numpy.get_include()]文件的Extension()中添加setup.py。如果我根本不导入numpy并手动传递通过调用numpy.get_include()可以获得的字符串,那么一切正常。但这肯定不是应该的。

0 个答案:

没有答案