如何让Cython在Enthought Canopy分发中使用MinGW
我刚下载了Enthought的Canopy学术版,并安装了Cython和MinGW(还有很多其他的包),现在想在ipython笔记本中通过单元魔法%%cython使用我之前写的一些cython代码。另外,我使用的是64位的Windows 7。
但是我遇到了这个问题:
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在EPD中使用mingw呢?
3 个回答
到目前为止,我的解决办法是完全卸载EPD和Anaconda,然后重新安装Anaconda,这样一切又能正常工作了。起初我以为这是因为Anaconda和EPD不太兼容。但我也试过再次卸载Anaconda,然后安装EPD,结果还是出现同样的错误。我确认mingw已经安装,并且这次安装EPD时,它自动添加到了我的路径中。
如果你想尝试重现这个错误,可以在64位的Windows 7上安装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.
我还在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吗?
我正在使用Enthought Canopy的学术版,遇到了和你一样的问题。
我通过将系统环境变量中的 VS90COMNTOOLS
设置为 C:/program files (x86)/Microsoft Visual Studio 12.0/Common7/Tools
来解决了这个问题(我在使用Windows 8.1 x64的VS2013专业版)。
我还将 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