编译第一个Cython程序时出现错误

4

我想开始使用Cython并尝试编译我的第一个程序。我已经创建了一个名为hello.pyx的文件并添加了以下代码:

def show():
    print ("Hello World")

并且有一个包含以下代码的 setup.py 文件:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("hello", ["hello.pyx"])]

setup(
    name = 'Hello world app',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)

这个问题涉及到我放在C:\Python32\cython programs文件夹中的两个程序以及Cython所在的路径C:\Python32\Lib\site-packages\Cython。然而,当我运行setup.py时,会出现以下错误:

Traceback (most recent call last):
  File "C:\Python32\cython programs\setup.py", line 10, in <module>
    ext_modules = ext_modules
  File "C:\Python32\lib\distutils\core.py", line 136, in setup
    raise SystemExit(gen_usage(dist.script_name) + "\nerror: %s" % msg)
SystemExit: usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help

error: no commands supplied

我猜我可能错过了一些非常简单的东西,但似乎找不到是什么。任何帮助都将不胜感激。


你是不是在命令行中没有给setup.py传递任何参数? - Joel Cornett
1
根据Cython文档,您必须使用build_ext参数。如果这是您的问题,您应该接受@Henry Gomersall的答案。 - Joel Cornett
2个回答

5
运行 python setup.py build_ext 命令。这对我来说有效。

谢谢,我想我成功地运行了我的setup.py,但仍然存在问题。一开始当我运行setup.py build_ext时,我遇到了错误:error: C:\Documents and Settings\student\hello.pyx: No such file or directory。之后我将hello.pyx的副本复制到student中并再次运行它。然后我得到了running build_ext; cythoning hello.pyx to hello.c; building 'hello' extension; error: Unable to find vcvarsall.bat; 我认为我需要以某种方式链接我的C编译器,并且我已经下载了MinGw,但我不确定我需要改变什么。 - enderx1x
你需要将mingw添加到你的路径中。我实际上使用msys( http://www.mingw.org/wiki/MSYS) 进行所有的Windows Python开发,它是mingw和其他有用的Unix工具的补充外壳。它让Windows的体验更加令人愉悦! 你可能需要在你的Windows路径变量中添加一些Python路径(我忘记了)。但一定要将你的shell字体从邪恶的默认设置中更改! 配置好后, 它非常适合使用easy_install等命令以及实际运行Python应用程序。 - Henry Gomersall
我还没有尝试过msys,但我已经将mingw添加到了我的路径中。再次运行后,我得到了以下错误:C:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -IC:\Python32\include -IC:\Python32\PC -c hello.c -o build\temp.win32-3.2\Release\hello.o; cc1.exe: error: unrecognized command line option '-mno-cygwin'; error: command 'gcc' failed with exit status 1。另外,有没有办法让我不必在student中复制hello.pyx文件? - enderx1x
你的目录结构是什么样子的?另外,你使用的distutils版本是什么? - Henry Gomersall
似乎在较新版本的gcc中,distutils存在一个错误:http://bugs.python.org/issue12641。尝试编辑`c:\Python27\Lib\distutils\cygwinccompiler.py`中的set_executables定义,以删除-mno-cygwin。此外,我真的非常建议安装和使用msys。 - Henry Gomersall
好的,我编辑了 c:\Python27\Lib\distutils\cygwinccompiler.py 文件,现在它可以工作了。谢谢你的帮助,我会看看 msys。 - enderx1x

0
我在这里猜测……但我认为是你目录名中的空格。我刚花了几分钟谷歌Cython和Distutils,因为我似乎记得其中一个建议在使用Windows时不要在目录名称中使用空格——但我没有找到我正在寻找的链接。
试着快速测试一下,将你的“cython程序”重命名为“cythonprograms”,看看会发生什么。

嗯,我认为这不应该有影响,因为路径被引号括起来了...我想他只是没有给脚本传递任何参数。 - Joel Cornett
我一开始也是这么想的,但如果你只运行“python setup.py”而没有参数,你会得到标准的distutils帮助……但你不会得到异常……他发布了一个回溯。 - user590028

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接