【发布时间】:2018-09-25 00:04:58
【问题描述】:
我有一个具有各种功能的 cython 脚本 example1.pyx。我正在尝试在我的 python 脚本中使用这些函数之一。为此,我只是尝试从 cython 模块导入函数,如下所示:
from example1 import myfunction1
我正在使用 Spyder 运行我的 python 脚本。当我运行它时,我收到以下错误
ModuleNotFoundError: No module named 'example1'
由于某种原因,.pyx 扩展脚本不被识别为可以导入的模块。我可以成功导入其他 python 脚本,但没有 cython .pyx 脚本。原来我没有编译 python 脚本。所以我使用了以下脚本:
from setuptools import setup, Extension
import numpy
from Cython.Distutils import build_ext as _build_ext
list_pyx = ['cydtw', 'cygak', 'cycc', 'soft_dtw_fast']
ext = [Extension(
'%s' % s, ['%s.pyx' % s], include_dirs=[numpy.get_include()]
) for s in list_pyx]
setup(
include_dirs=[numpy.get_include()],
ext_modules=ext,
cmdclass={'build_ext': _build_ext},
)
当我运行它时,我收到错误消息说我需要 Visual C++ 14.0 编译器。查看https://wiki.python.org/moin/WindowsCompilers 中的文档,我已经安装的Visual Studio 15 应该有Visual C++ 14.0,所以这很混乱。所以我更新了我的设置工具。摆脱了那个错误,但事实证明我找不到文件路径 C;\program files\Microsoft SDKs\Windows\v8.1\lib。我可以看到它没有。所以这就是我现在卡住的地方。有什么线索吗?
【问题讨论】:
-
你有compiled it,对吧?
-
我刚刚从存储库下载了 cython 脚本。在我的python脚本中使用它之前我需要编译它吗?除非编译,否则python不会将其识别为模块吗?
-
是的 - Cython 的重点在于它将代码转换为 C 语言,使其运行速度更快并直接访问 C 函数。这意味着它需要编译。您下载它的存储库可能有一些安装说明,因此您应该遵循它们。
-
很遗憾没有可用的安装说明。所以我查找了如何编译 cython 脚本,这里有一个很好的教程:
-
link。所以我创建了一个 setup.py 文件并使用 setup.py build_ext 构建它,但得到一个错误,说找不到 vcvarsall.bat。这似乎与 python 如何引用错误的 Visual Studio 版本有关。作为背景,我正在使用 Ananconda。 Windows 7 和 Visual Studio 15
标签: python import module cython spyder