如何让Cython在Enthought Canopy发行版中使用MinGW

3

我刚刚下载了Enthought的Canopy学术版并安装了Cython和MinGW(以及很多其他软件包),希望通过单元格魔法%%cython在ipython笔记本中使用之前编写的一些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
我该如何在EPD中让Cython使用mingw?

还尝试了这个路径 C:\Users\Patrick\User\Lib\site-packages\mingw-4.8.1-2.egg\EGG-INFO\usr\bin,应该是正确的位置。以前在Anaconda中让mingw工作,我还记得必须在disutils.cfg[build][build_ext]下设置compiler=mingw32,但是在EPD中找不到这个文件。有人能告诉我在哪里找到这个文件,这样我也可以尝试一下吗? - pbreach
尝试了 %%cython -c=mingw32%%cython -compiler=mingw,但都没有成功。安装了 Visual Studio 2008 Express Edition,但出现了相同的错误,并附加了一条注释,说明对于64位系统需要完整版本而不是Express版本。这真是让人头疼。 - pbreach
我正在使用Canopy和MinGW的组合,没有任何问题,请参考我对这个问题给出的答案。 - Philipp Schwarz
3个回答

2
我正在使用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似乎非常不错,特别是安装一些有很多依赖关系的软件包时。我也一直在使用学术版本,所以我希望它能正常工作。 - pbreach

1

目前,我的解决方案是完全卸载EPD和Anaconda,然后重新安装Anaconda,这样一切都能正常工作。起初我以为原因是Anaconda和EPD不能很好地协作。但我也试过再次卸载Anaconda,然后安装EPD,仍然出现同样的错误。我确认mingw已经安装,并且在此次安装EPD时自动添加到了路径中。

如果您想尝试重现此错误,请在Windows 7 64位上安装EPD canopy-1.4.0-win-64,并尝试在IPython Notebook中使用%%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.

我还在C:\Users\Patrick\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.0.1938.win-x86_64\Lib\distutils中添加了distutils.cfg,内容如下:

[build]
compiler = mingw32

[build_ext]
compiler = mingw32

但问题依然存在。有人能在所述平台上使用 %%cython 而不需要完整版的 VS 2008 吗?


0

根据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笔记本中使用cython并使用disutils构建cython模块。这一定是EPD的某种配置问题(Anaconda也使用MinGW)。 - pbreach

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接