【问题标题】:How to use Cython on Windows 10 with python 3.8如何在带有 python 3.8 的 Windows 10 上使用 Cython
【发布时间】:2020-02-20 14:52:38
【问题描述】:

我想知道如何在 Cython 的帮助下正确地将 py 文档转换为 C 但总是有一些错误。

D:\Cython\test4>python setup.py build_ext --inplace
running build_ext
Traceback (most recent call last):
  File "setup.py", line 4, in <module>
    setup(ext_modules = cythonize('hello.pyx', compiler_directives={'language_level': 3}))
  File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38\lib\distutils\dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38\lib\distutils\dist.py", line 985, in run_command
    cmd_obj.run()
  File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38\lib\distutils\command\build_ext.py", line 306, in run
    self.compiler = new_compiler(compiler=self.compiler,
  File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38\lib\distutils\ccompiler.py", line 1032, in new_compiler
    return klass(None, dry_run, force)
  File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38\lib\distutils\cygwinccompiler.py", line 282, in __init__
    CygwinCCompiler.__init__ (self, verbose, dry_run, force)
  File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38\lib\distutils\cygwinccompiler.py", line 157, in __init__
    self.dll_libraries = get_msvcr()
  File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38\lib\distutils\cygwinccompiler.py", line 86, in get_msvcr
    raise ValueError("Unknown MS Compiler version %s " % msc_ver)
ValueError: Unknown MS Compiler version 1916

我有 hello.pyx 文件,其中包含此代码

cpdef int test(int x):
    cdef int y = 1
    cdef int i
    for i in range(1, x+1):
        y *= i
    return y

Setup.py 文件包括

   from distutils.core import setup 
   from Cython.Build import cythonize
   setup(ext_modules = cythonize('hello.pyx', compiler_directives={'language_level': 3}))

Microsoft Visual Studio 已安装,gcc 编译器也已安装。我该怎么办?

【问题讨论】:

  • 好像和MS Compiler有关;我不确定问题出在哪里,但您可以尝试从 Visual Studio 开发人员命令提示符中执行此操作。
  • 您需要获得支持的编译器。 MinGW 仅在 Python3.4 之前有效:wiki.python.org/moin/WindowsCompilers#GCC_-MinGW-w64.28x86.2C_x64.29
  • 这只是您需要特定版本的 Windows 编译器来匹配使用wiki.python.org/moin/WindowsCompilers 构建的 Python 的常见问题吗

标签: c gcc cython cythonize


【解决方案1】:

嘿,我找到了简单的解决方案。只需安装 Microsfot Visual Studio(社区版有效)。确保检查了MSVCWindows 10 SDK。有人告诉我两者都是必需的。

选择Desktop C++ development

选择 MSVC 和 Win10 SDK:

安装后重启电脑。它应该用python setup.py build_ext --incplace编译

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 2020-03-27
    • 2020-12-14
    • 2020-09-03
    • 2021-03-28
    • 2020-04-08
    • 2020-02-21
    相关资源
    最近更新 更多