【问题标题】:How to set up a windows cython environment from scratch?如何从头开始设置 windows cython 环境?
【发布时间】:2020-12-15 15:44:27
【问题描述】:

我已经在互联网上搜索了几个小时以找出如何使 cython 工作但没有成功。

我在 windows 上,我使用 python3 和 Anaconda,我也有使用 mingw GCC 编译器的代码块。

我希望能够使用 cython 运行最基本的代码(例如“helloworld”),因为到目前为止我只遇到过我不理解的错误。他们可能来自不稳定的开发环境,所以这就是我问这个问题的原因。

主要错误发生在将 .pyx 代码转换为 .c 时,即使我实现了在生成的 .c 上还有更多错误。


这是我的“helloworld.pyx”:

print('Hello Wolrd')

这是我的 setup.py :

from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize('helloworld.pyx'))

当我运行python3 setup.py 时,我得到ModuleNotFoundError: No module named 'Cython'

当我运行 cython helloworld.pyx 时,我得到一个文件 helloworld.c 但是当我在代码块中运行它时,我得到了错误Python.h : no such file or directory

所以我通过将绝对路径放在包含命令中来临时修复它,但我仍然得到错误。


我也尝试过使用python setup.py build_ext --inplace,但我明白了

running build_ext
building 'helloworld' extension
C:\MinGW\bin\gcc.exe -mdll -O -Wall -DMS_WIN64 -IC:\Users\Julien\anaconda3\include -IC:\Users\Julien\anaconda3\include -c helloworld.c -o build\temp.win-amd64-3.7\Release\helloworld.o
In file included from C:\Users\Julien\anaconda3\include/Python.h:87:0,
                 from helloworld.c:4:
C:\Users\Julien\anaconda3\include/pytime.h:123:59: warning: 'struct timeval' declared inside parameter list
C:\Users\Julien\anaconda3\include/pytime.h:123:59: warning: its scope is only this definition or declaration, which is probably not what you want
C:\Users\Julien\anaconda3\include/pytime.h:131:5: warning: 'struct timeval' declared inside parameter list
C:\Users\Julien\anaconda3\include/pytime.h:136:5: warning: 'struct timeval' declared inside parameter list
helloworld.c:201:41: warning: division by zero
helloworld.c:201:12: error: enumerator value for '__pyx_check_sizeof_voidp' is not an integer constant
error: command 'C:\\MinGW\\bin\\gcc.exe' failed with exit status 1

我按照教程安装了 mingw(它适用于代码块中的 C 编码,所以我应该正确安装它)并且我验证了 Cython 已正确安装在 Python 库中。我做错了什么??

【问题讨论】:

  • wiki.python.org/moin/WindowsCompilers。使用 MSVC 会好得多。看起来您可能将 Cython 安装在与您用于运行 setup.py 的环境不同的 conda 环境中。
  • 这解决了我的问题。我使用您给我的网页上的解释正确地重新安装了,并且经过更多的工作,它现在运行良好。谢谢!

标签: cython development-environment


【解决方案1】:

好的,问题出在编译器上。

问题是因为我的 mingw 编译器不正确:我建议遵循 these instructions,您可以在 https://wiki.python.org/moin/WindowsCompilers 找到。

/!\ 不要忘记在你的 python 环境中安装 setuptools(使用给定的命令对我有用)

这至少应该允许您为 cython 制作 basic tutorial

【讨论】:

    猜你喜欢
    • 2011-09-29
    • 2010-09-29
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多