【问题标题】:Can't Compile Cython on Windows with MingW无法使用 MingW 在 Windows 上编译 Cython
【发布时间】:2019-03-22 03:43:54
【问题描述】:

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

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 代码。我已经尝试了我能想到的一切,但不知道出了什么问题。关于如何解决这个问题的任何想法?

【问题讨论】:

  • 您使用的是使用 Mingw 编译的 Python 版本吗?如果你不是,那么我认为这是不兼容的。有一个名为 Mingwpy 的项目与 Python 3.4 兼容,但我认为在那之后他们遇到了无法解决的问题。不过,我不是 100% 了解最新情况

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


【解决方案1】:

尝试改变

elif msc_ver == '1914':

    return ['vcruntime140']

to  return ['msvcr100']

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多