codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Where do I put my cython files in a python distribution?


By : , Category : python

Putting the .pyx files in the same directory as .py files makes the most sense to me. It's what the authors of scikit-learn have done and what I've done in my py-earth module. I guess I think of Cython modules as optimized replacements for Python modules. I will often begin by writing a package in pure Python, then replace some modules with Cython if I need better performance. Since I'm treating Cython modules as replacements for Python modules, it makes sense to me to keep them in the same place. It also works well for test builds using the --inplace argument.

ReLated :

Regarding your second error, you have a typo:

print str(tripeleit(3))

It should be:

print str(tripleit(3))

You can pass multiple extensions like:

extensions = [Extension('test', ['test.pyx']),
              Extension('test2', ['test2.pyx'])]

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
cpdef f(int x):
    cdef int y = x+2*100
    return y')

C:UsersPatrickUserlibsite-packagesIPythoncoreinteractiveshell.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:UsersPatrickUserlibsite-packagesIPythonextensionscythonmagic.pyc in cython(self, line, cell)

C:UsersPatrickUserlibsite-packagesIPythoncoremagic.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:UsersPatrickUserlibsite-packagesIPythonextensionscythonmagic.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:UsersPatrickAppDataLocalEnthoughtCanopyAppappdatacanopy-1.4.0.1938.win-x86_64libdistutilscommanduild_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:UsersPatrickAppDataLocalEnthoughtCanopyAppappdatacanopy-1.4.0.1938.win-x86_64libdistutilscommanduild_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:UsersPatrickAppDataLocalEnthoughtCanopyAppappdatacanopy-1.4.0.1938.win-x86_64libdistutilscommanduild_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:UsersPatrickAppDataLocalEnthoughtCanopyAppappdatacanopy-1.4.0.1938.win-x86_64libdistutilsmsvc9compiler.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:UsersPatrickAppDataLocalEnthoughtCanopyAppappdatacanopy-1.4.0.1938.win-x86_64libdistutilsmsvc9compiler.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:UsersPatrickAppDataLocalEnthoughtCanopyAppappdatacanopy-1.4.0.1938.win-x86_64libdistutilsmsvc9compiler.py in query_vcvarsall(version, arch)
    304     if vs_info is None:
    305         raise DistutilsPlatformError(
--> 306             '
'.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:UsersPatrickAppDataLocalEnthoughtCanopyAppappdatacanopy-1.4.0.1938.win-x86_64Libdistutils 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?

I don't think you can (http://docs.cython.org/src/userguide/sharing_declarations.html#sharing-extension-types) but you can work-around it using __cinit__ to assert that the attribute has the correct type:

In your Cython file (named "p.pyx" for example):

import my_python_module as q

cdef class Y:
    cdef int i
    cdef public object x  # public so it can be accessed from Python

    def __cinit__(self, x_):
        assert isinstance(x_, q.X)
        self.x = x_

and my_python_module.py is where you have defined your class X:

class X(object):
    def __init__(self):
        self.i = 1

Then, you use it like this:

import my_python_module as q
import p

y = p.Y(q.X())
print y.x
print y.x.i
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .