简体   繁体   中英

How to get cython to use MinGW with enthought canopy distribution

I just downloaded Enthought's Canopy academic version and installed Cython and MinGW (along with a lot of other packages) and want to use some cython code in the ipython notebook via cell magic %%cython that I had written previously. Also I am using Windows 7 64-bit.

Except I get this:

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.

This is mentioned right in the Cython docs that this will happen if mingw is not added to PATH. I feel like this was much easier with Anaconda but here's what I've done so far:

I've tried adding these to my path:

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

What do I have to do to get Cython to use mingw with EPD?

I am using the academic version of enthought canopy and I had the same issue as you.

I solved this by setting VS90COMNTOOLS in the system environnement variable to C:/program files (x86)/Microsoft Visual Studio 12.0/Common7/Tools (I am using VS2013 Pro in windows 8.1 x64)

I have also added the path to vcvarsall.bat whch in my case is: C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC to system environnement variables

run vcvarsall.bat in command prompt, then python setup.py build_ext --inplace and it should work

EDIT:

I have tested it with this and worked:

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

Well, so far my solution has been to completely uninstall EPD and Anaconda, then reinstall Anaconda and everything works again. At first I thought the reason for this is that Anaconda and EPD don't play nicely together. But I also tried uninstalling Anaconda again then installing EPD and still get the same error. I confirmed that mingw is installed and it was automatically appended to my path when installing EPD this time.

If you want to try to recreate this error try installing EPD canopy-1.4.0-win-64 on Windows 7 64-bit and try creating a simple cython function in the ipython notebook using %%cython cell magic for example:

%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.

I've also added distutils.cfg to C:\\Users\\Patrick\\AppData\\Local\\Enthought\\Canopy\\App\\appdata\\canopy-1.4.0.1938.win-x86_64\\Lib\\distutils with the following:

[build]
compiler = mingw32

[build_ext]
compiler = mingw32

but still same issue. Has anyone been able to use %%cython in ipython for the platform mentioned without having the full version of VS 2008?

The combination MinGW, 64-bit and Cython does not work according to https://github.com/cython/cython/wiki/64BitCythonExtensionsOnWindows and given my own experience. (Also helpful is this 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 )

You ask if the full version of VS2008 is needed. My understanding is that the combination of the Visual Studio Express edition and the 64bit SDK (references in the link) should be enough. VS2008 is available from https://www.dreamspark.com/Product/Product.aspx?productid=34 and the SDK here http://www.microsoft.com/en-us/download/details.aspx?id=24826 . Remember to select 64bit compiler when installing the SDK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM