无法使用MingW在Windows上编译Cython

时间:2019-03-22 03:43:55

标签: python python-3.x windows mingw cython

在具有Python 3.7.0的Windows 10 Build 14393上,位于C:\MinGW的Cython 0.29.6和MinGW32在系统C:\MinGW\bin环境变量 I上的路径为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代码。我已经尽力想了一切,不知道出了什么问题。有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:0)

尝试更改

elif msc_ver == '1914':

    return ['vcruntime140']

to  return ['msvcr100']

这对我有用,虽然真的不知道为什么