【问题标题】:How to get cython to use MinGW with enthought canopy distribution如何让 cython 使用具有经过深思熟虑的树冠分布的 MinGW
【发布时间】:2014-06-21 18:07:03
【问题描述】:

我刚刚下载了 Enthought 的 Canopy 学术版并安装了 Cython 和 MinGW(以及许多其他软件包),并想通过我之前编写的 cell magic %%cython 在 ipython notebook 中使用一些 cython 代码。我也在使用 Windows 7 64 位。

除非我得到这个:

DistutilsPlatformError: Could not find Visual Studio 2008 in your path.

If you do not have Visual Studio 2008 installed, you can use
the MinGW compiler instead. To install mingw, do:
    enpkg mingw
To use the MinGW compiler to build an extension module, use
the '-c' flag, e.g.:
    python setup.py build_ext -c mingw64
Note that building Python extensions with MinGW is not officially
supported, although it is known to work in many cases.

这在 Cython 文档中已经提到,如果没有将 mingw 添加到 PATH,就会发生这种情况。我觉得使用 Anaconda 时这要容易得多,但这是我到目前为止所做的:

我已经尝试将这些添加到我的路径中:

C:\Users\Patrick\User\EGG-INFO\mingw\usr\x86_64-w64-mingw32\bin

C:\Users\Patrick\User\EGG-INFO\mingw\usr\bin

C:\Users\Patrick\User\Lib\site-packages\mingw-4.8.1-2.egg-info\scripts

我必须做些什么才能让 Cython 将 mingw 与 EPD 一起使用?

【问题讨论】:

  • 也试过这个C:\Users\Patrick\User\Lib\site-packages\mingw-4.8.1-2.egg\EGG-INFO\usr\bin,它应该是正确的位置。以前在 Anaconda 中让 mingw 工作,我还记得必须在 [build][build_ext] 下设置 compiler=mingw32 disutils.cfg 但是这个文件在 EPD 中找不到。谁能告诉我在哪里可以找到此文件,以便我也可以尝试一下?
  • 试过 %%cython -c=mingw32%%cython -compiler=mingw 没有成功。安装了 Visual Studio 2008 Express Edition,它给出了同样的错误,并附注说 64 位系统需要完整版本,而不是 express 版本。这正在成为一种真正的痛苦。

标签: python mingw cython enthought


【解决方案1】:

我用的是学术版的enthought canopy,遇到了和你一样的问题。

我通过将系统环境变量中的VS90COMNTOOLS 设置为C:/program files (x86)/Microsoft Visual Studio 12.0/Common7/Tools 解决了这个问题(我在windows 8.1 x64 中使用VS2013 Pro)

我还添加了vcvarsall.bat 的路径,在我的例子中是:C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC 到系统环境变量

在命令提示符下运行vcvarsall.bat,然后运行python setup.py build_ext --inplace,它应该可以工作

编辑:

我已经对此进行了测试并工作了:

In [1] : %load_ext cythonmagic

In [2] : %%cython
         def fib(int n):
             cdef int i, a, b
             a, b = 1, 1
             for i in range(n):
                 a, b = a+b, a
             return a

In [3] : fib(10)
Out[3] : 144

【讨论】:

  • 太棒了!我得在周末的某个时候试试这个。我一直在使用 Anaconda,但 EPD 似乎相当不错,尤其是对于安装一些具有大量依赖项的软件包。我也在使用学术版,所以我希望它能工作
【解决方案2】:

嗯,到目前为止,我的解决方案是完全卸载 EPD 和 Anaconda,然后重新安装 Anaconda,然后一切正常。起初我认为这是因为 Anaconda 和 EPD 不能很好地结合在一起。但我也尝试再次卸载 Anaconda,然后安装 EPD,但仍然出现相同的错误。我确认安装了mingw,这次安装EPD时它自动附加到我的路径中。

如果您想尝试重现此错误,请尝试在 Windows 7 64 位上安装 EPD canopy-1.4.0-win-64,并尝试在 ipython 笔记本中使用 %%cython 单元魔法创建一个简单的 cython 函数:

%load_ext cythonmagic

In [18]:

%%cython
cimport cython
cpdef f(int x):
    cdef int y = x+2*100
    return y

---------------------------------------------------------------------------
DistutilsPlatformError                    Traceback (most recent call last)
<ipython-input-18-326d9aaeb05c> in <module>()
----> 1 get_ipython().run_cell_magic(u'cython', u'', u'cimport cython\ncpdef f(int x):\n    cdef int y = x+2*100\n    return y')

C:\Users\Patrick\User\lib\site-packages\IPython\core\interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
   2160             magic_arg_s = self.var_expand(line, stack_depth)
   2161             with self.builtin_trap:
-> 2162                 result = fn(magic_arg_s, cell)
   2163             return result
   2164 

C:\Users\Patrick\User\lib\site-packages\IPython\extensions\cythonmagic.pyc in cython(self, line, cell)

C:\Users\Patrick\User\lib\site-packages\IPython\core\magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

C:\Users\Patrick\User\lib\site-packages\IPython\extensions\cythonmagic.pyc in cython(self, line, cell)
    266             build_extension.build_temp = os.path.dirname(pyx_file)
    267             build_extension.build_lib  = lib_dir
--> 268             build_extension.run()
    269             self._code_cache[key] = module_name
    270 

C:\Users\Patrick\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.0.1938.win-x86_64\lib\distutils\command\build_ext.py in run(self)
    335 
    336         # Now actually compile and link everything.
--> 337         self.build_extensions()
    338 
    339     def check_extensions_list(self, extensions):

C:\Users\Patrick\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.0.1938.win-x86_64\lib\distutils\command\build_ext.py in build_extensions(self)
    444 
    445         for ext in self.extensions:
--> 446             self.build_extension(ext)
    447 
    448     def build_extension(self, ext):

C:\Users\Patrick\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.0.1938.win-x86_64\lib\distutils\command\build_ext.py in build_extension(self, ext)
    494                                          debug=self.debug,
    495                                          extra_postargs=extra_args,
--> 496                                          depends=ext.depends)
    497 
    498         # XXX -- this is a Vile HACK!

C:\Users\Patrick\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.0.1938.win-x86_64\lib\distutils\msvc9compiler.py in compile(self, sources, output_dir, macros, include_dirs, debug, extra_preargs, extra_postargs, depends)
    512 
    513         if not self.initialized:
--> 514             self.initialize()
    515         compile_info = self._setup_compile(output_dir, macros, include_dirs,
    516                                            sources, depends, extra_postargs)

C:\Users\Patrick\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.0.1938.win-x86_64\lib\distutils\msvc9compiler.py in initialize(self, plat_name)
    422                             PLAT_TO_VCVARS[plat_name]
    423 
--> 424             vc_env = query_vcvarsall(VERSION, plat_spec)
    425 
    426             # take care to only use strings in the environment.

C:\Users\Patrick\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.0.1938.win-x86_64\lib\distutils\msvc9compiler.py in query_vcvarsall(version, arch)
    304     if vs_info is None:
    305         raise DistutilsPlatformError(
--> 306             '\n'.join((VS_NOT_FOUND_MESSAGE, MINGW_DEFLECT_MESSAGE)))
    307 
    308     vcvarsall, is_express = vs_info

DistutilsPlatformError: Could not find Visual Studio 2008 in your path.

If you do not have Visual Studio 2008 installed, you can use
the MinGW compiler instead. To install mingw, do:
    enpkg mingw
To use the MinGW compiler to build an extension module, use
the '-c' flag, e.g.:
    python setup.py build_ext -c mingw64
Note that building Python extensions with MinGW is not officially
supported, although it is known to work in many cases.

我还添加了 distutils.cfgC:\Users\Patrick\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.0.1938.win-x86_64\Lib\distutils 并带有以下内容:

[build]
compiler = mingw32

[build_ext]
compiler = mingw32

但仍然是同样的问题。在没有完整版 VS 2008 的情况下,是否有人能够在 ipython 中为提到的平台使用 %%cython

【讨论】:

    【解决方案3】:

    根据https://github.com/cython/cython/wiki/64BitCythonExtensionsOnWindows 和我自己的经验,MinGW、64 位和 Cython 的组合不起作用。 (这个http://www.mathworks.com/matlabcentral/answers/98351-how-can-i-set-up-microsoft-visual-studio-2008-express-edition-for-use-with-matlab-7-7-r2008b-on-64也很有帮助)

    你问是否需要完整版的VS2008。我的理解是Visual Studio Express版和64位SDK(链接中的参考资料)的组合应该足够了。 VS2008 可从https://www.dreamspark.com/Product/Product.aspx?productid=34 获得,SDK 可在此处http://www.microsoft.com/en-us/download/details.aspx?id=24826 获得。安装 SDK 时记得选择 64 位编译器。

    【讨论】:

    • 自从我回到 Anaconda 后,我就能够在 ipython notebook 中使用 cython 并使用 disutils 构建 cython 模块。一定是 EPD 的某种配置问题(Anaconda 也使用 MinGW)。
    猜你喜欢
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多