如何让cython使用MinGW与enthought冠层分布

时间:2014-06-21 18:07:03

标签: python mingw cython enthought

我刚下载了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

我需要做些什么才能让Cython使用mingw和EPD?

3 个答案:

答案 0 :(得分:2)

我正在使用enthought冠层的学术版本,我和你有同样的问题。

我通过将系统环境变量中的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

答案 1 :(得分:1)

好吧,到目前为止,我的解决方案是完全卸载EPD和Anaconda,然后重新安装Anaconda,一切都恢复正常。起初我认为原因是Anaconda和EPD不能很好地在一起玩。但我也尝试再次卸载Anaconda然后安装EPD仍然得到相同的错误。我确认安装了mingw,这次安装EPD时会自动附加到我的路径上。

如果您想尝试重新创建此错误,请尝试在Windows 7 64位上安装EPD canopy-1.4.0-win-64并尝试使用%%cython单元魔法在ipython笔记本中创建一个简单的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.cfg添加到C:\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

但仍然是同一个问题。有没有人能够在ipython中使用%%cython来获得没有完整版VS 2008的平台?

答案 2 :(得分:0)

MinGW,64位和Cython的组合根据https://github.com/cython/cython/wiki/64BitCythonExtensionsOnWindows不起作用并且给出了我自己的经验。 (这也很有用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位编译器。