【问题标题】:Spyder does not recognise cython modules when importing in a script在脚本中导入时 Spyder 无法识别 cython 模块
【发布时间】: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


【解决方案1】:

设法解决了这个问题。没有在 Visual Studio 2015 上安装 Visual C++ 编译器。

要检查,请打开 Visual Studio 15。

查看 Visual C++ 下的模板。

我可以看到需要安装的 Visual C++ 2015 Tools for Windows Desktop。

这包括适用于 Windows 桌面的工具和库,其中包括编译器、通用 CRT 和 windows 8.1 SDK。运行安装。同时它会要求你关闭视觉工作室。

能够编译 cython 脚本以及在我的 python 项目中调用脚本中的函数。

我会说你必须小心你的编译器版本和python版本匹配。可能存在兼容性问题。

有关 Visual Studio 构建工具的详细信息可在此处获得: https://landinghub.visualstudio.com/visual-cpp-build-tools

对于安装非纯 python 包或编译 cython 或 pyrex 文件,兼容性方面的详细信息如下: https://wiki.python.org/moin/WindowsCompilers

【讨论】:

  • 所以您是否仍然需要在您尝试将 cython 模块导入的脚本中显式使用编译代码,或者是否足以在您的系统上安装 Visual Studio,然后再导入以前返回错误的命令现在可以工作了吗?
猜你喜欢
  • 2019-11-10
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多