python - 无法在 Windows 上使用 MingW 编译 Cython

标签 python python-3.x windows mingw cython

在 Windows 10 Build 14393 上,使用 Python 3.7.0、Cython 0.29.6 和 MinGW32,位于 C:\MinGW ,路径为 C:\MinGW\bin系统 PATH 环境变量,我无法使用 MingW 编译 Cython 代码。当我尝试这样做时,我收到以下消息:

running build_ext
building 'abc_image_stats' extension
error: Unable to find vcvarsall.bat
c:\program files\python\python37\lib\site-packages\Cython\Compiler\Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: C:\Users\abc\Downloads\python\abc_image_stats\abc_image_stats.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)

我尝试了各种修复,例如提到的 here ,但无济于事。按照该解决方案中的说明制作配置文件后(但在更改 cgwinccompiler.py 之前),我尝试再次编译,但收到此错误:

running build_ext
Traceback (most recent call last):
  File "setup.py", line 7, in <module>
    include_dirs=[numpy.get_include()] # https://stackoverflow.com/a/14657667
  File "C:\Program Files\Python\Python37\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Program Files\Python\Python37\lib\distutils\dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "C:\Program Files\Python\Python37\lib\distutils\dist.py", line 985, in run_command
    cmd_obj.run()
  File "C:\Program Files\Python\Python37\lib\distutils\command\build_ext.py", line 308, in run
    force=self.force)
  File "C:\Program Files\Python\Python37\lib\distutils\ccompiler.py", line 1031, in new_compiler
    return klass(None, dry_run, force)
  File "C:\Program Files\Python\Python37\lib\distutils\cygwinccompiler.py", line 282, in __init__
    CygwinCCompiler.__init__ (self, verbose, dry_run, force)
  File "C:\Program Files\Python\Python37\lib\distutils\cygwinccompiler.py", line 157, in __init__
    self.dll_libraries = get_msvcr()
  File "C:\Program Files\Python\Python37\lib\distutils\cygwinccompiler.py", line 86, in get_msvcr
    raise ValueError("Unknown MS Compiler version %s " % msc_ver)
ValueError: Unknown MS Compiler version 1914
c:\program files\python\python37\lib\site-packages\Cython\Compiler\Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: C:\Users\abc\Downloads\python\abc_image_stats\abc_image_stats.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)

然后我做了最后两个步骤;我添加了

elif msc_ver == '1914':
            # Visual Studio 2015 / Visual C 14.0
            # "msvcr140.dll no longer exists" http://blogs.msdn.com/b/vcblog/archive/2014/06/03/visual-studio-14-ctp.aspx
            return ['vcruntime140']

到 cygwinccompiler.py 文件并将 vcruntime140.dll 复制到 C:\Program Files\Python\Python37\libs 文件夹,但随后我在尝试编译时收到此错误:

running build_ext
building 'abc_image_stats' extension
C:\MinGW\bin\gcc.exe -mdll -O -Wall "-IC:\Program Files\Python\Python37\lib\site-packages\numpy\core\include" "-IC:\Program Files\Python\Python37\include" "-IC:\Program Files\Python\Python37\include" -c abc_image_stats.c -o build\temp.win-amd64-3.7\Release\abc_image_stats.o
In file included from C:\Program Files\Python\Python37\include/Python.h:68:0,
                 from abc_image_stats.c:4:
C:\Program Files\Python\Python37\include/pytime.h:123:59: warning: 'struct timeval' declared inside parameter list will not be visible outside of this definition or declaration
 PyAPI_FUNC(int) _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv);
                                                           ^~~~~~~
C:\Program Files\Python\Python37\include/pytime.h:130:12: warning: 'struct timeval' declared inside parameter list will not be visible outside of this definition or declaration
     struct timeval *tv,
            ^~~~~~~
C:\Program Files\Python\Python37\include/pytime.h:135:12: warning: 'struct timeval' declared inside parameter list will not be visible outside of this definition or declaration
     struct timeval *tv,
            ^~~~~~~
writing build\temp.win-amd64-3.7\Release\abc_image_stats.cp37-win_amd64.def
C:\MinGW\bin\gcc.exe -shared -s build\temp.win-amd64-3.7\Release\abc_image_stats.o build\temp.win-amd64-3.7\Release\abc_image_stats.cp37-win_amd64.def "-LC:\Program Files\Python\Python37\libs" "-LC:\Program Files\Python\Python37\PCbuild\amd64" -lpython37 -lvcruntime140 -o C:\Users\abc\Downloads\python\abc_image_stats\abc_image_stats.cp37-win_amd64.pyd
C:\Program Files\Python\Python37\libs/libpython37.a: error adding symbols: File format not recognized
collect2.exe: error: ld returned 1 exit status
error: command 'C:\\MinGW\\bin\\gcc.exe' failed with exit status 1
c:\program files\python\python37\lib\site-packages\Cython\Compiler\Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: C:\Users\abc\Downloads\python\abc_image_stats\abc_image_stats.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)

需要明确的是,我的 setup.py 文件如下所示:

from distutils.core import setup
from Cython.Build import cythonize
import numpy

setup(
    ext_modules = cythonize("abc_image_stats.pyx"),
    include_dirs=[numpy.get_include()] # https://stackoverflow.com/a/14657667
)

我正在尝试通过命令行使用python setup.py build_ext --inplace进行编译。

我想做的就是使用 MingW 在 Windows 10 计算机上编译 Cython 代码。我已经尝试了所有我能想到的方法,但不知道出了什么问题。关于如何解决这个问题有什么想法吗?

最佳答案

尝试改变

elif msc_ver == '1914':

    return ['vcruntime140']

to  return ['msvcr100']

这对我有用,但不知道为什么

关于python - 无法在 Windows 上使用 MingW 编译 Cython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55292574/

相关文章:

python - 连接列表的元素

python - 如何部分读取文本文件并将其写入另一个文本文件

python - 理解为什么这个 python 代码随机工作

c++ - C++ 中的网络适配器信息

c++ - 如何强制编译器将 C 名称改写为 C++ 名称

python - Django : How to structure template folder considering template tag includes?

python - 为什么 Python 哈希列表不使用 ID?

python - 将 10 分钟和 30 分钟 NETCDF 时间序列转换为每小时 + 在 Python 中删除 NaN 的函数?

python - 访问字典中的函数第 2 部分

java - 是否可以在 Windows 中编写忽略所有形式的 SIGTERM 的批处理 [或其他可执行] 应用程序