codingdir logo sitemap sitemap |

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]:

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

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)
    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   
    269             self._code_cache[key] = module_name
    270 in run(self)
    336         # Now actually compile and link everything.
--> 337         self.build_extensions()
    339     def check_extensions_list(self, extensions): in build_extensions(self)
    445         for ext in self.extensions:
--> 446             self.build_extension(ext)
    448     def build_extension(self, ext): in build_extension(self, ext)
    494                                          debug=self.debug,
    495                                          extra_postargs=extra_args,
--> 496                                          depends=ext.depends)
    498         # XXX -- this is a Vile HACK! in compile(self, sources, output_dir, macros, include_dirs, debug, extra_preargs, extra_postargs, depends)
    513         if not self.initialized:
--> 514             self.initialize()
    515         compile_info = self._setup_compile(output_dir, macros, include_dirs,
    516                                            sources, depends, extra_postargs) in initialize(self, plat_name)
    422                             PLAT_TO_VCVARS[plat_name]
--> 424             vc_env = query_vcvarsall(VERSION, plat_spec)
    426             # take care to only use strings in the environment. in query_vcvarsall(version, arch)
    304     if vs_info is None:
    305         raise DistutilsPlatformError(
--> 306             '
    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 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 with the following:

compiler = mingw32

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 ( 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 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

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 © All Rights Reserved .