Skip to main content

gcc/distutils wrapper for auto-compiling Cython code (including inline)

Project description

Cpyx is a utility script to automatically run Cython and gcc with support for numpy.
It can also run inlined Cython code from within Python via an import mechanism (see CythonInlineTest.py).
I find this immensely helpful for testing simple Cython code, especially when using a GUI shell like PySlices or DreamPie.

The three core functions in Cpyx are Cdll, Cpyx, and CpyxLib.
Cdll is a simple gcc frontend, Cpyx runs cython and then compiles the c output, and CpyxLib compiles a .pyx file and links in an external .c file (useful when wrapping a single C source with Cython).
CythonInline directly compiles Cython code passed as a string (by generating a temp file and running Cpyx) and returns the python call to "import \*" the resulting module. Calling exec() will run the code inline (see CythonInlineTest.py).

For standard (non-inline) usage, if the directory is not set in the input file, Cpyx looks for the environment variable 'MYPYREX' and falls back to the current directory.
Correct file extensions (.pyx and .c) are assumed, so you can leave them off and they are automatically inserted.
With this in mind, calls to Cpyx can be very compact, i.e.:
Cpyx('CythonModule')
This compiles CythonModule.pyx to CythonModule.c and then to CythonModule.dll or libCythonModule.so.

By default, Cpyx makes direct system calls to Cython and gcc, with switches for the 3 different major OS's (Windows, Linux, Mac).
Still, some aspects of the paths are hard-coded, so you may have problems if cython or gcc (or libpython) in a non-standard place.
If this happens and Cpyx fails, you can either place a link to the real file in the spot Cpyx was looking for it in or just go into Cpyx.py and edit the paths at the top to match yours (just make sure you are looking at the right OS).

Also, Cpyx is quite verbose, outputting each command in sequence. So, you can also copy and manually run/modify the compiler commands in cmd/sh if you need to (and/or change the options inside Cdll/Cpyx/CpyxLib).
This way even if Cpyx doesn't quite work, maybe you can use it to help you learn how to use cython and gcc ;)
Cpyx is quite simple (the 3 main functions are <200 lines of total code), so it is easy to debug and work on.

Instead of direct system calls, Cpyx can also use distutils (set useDistutils=True in the function you are calling).
In this case, Cpyx builds you a form-based "setup.py" file and executes it automatically.
This can be handy if you want to transition from "play with some cython code" to actually putting it in a full-blown installable python package.

Ideally, Cpyx should work anywhere gcc, cython, and python/numpy are installed.
It is relatively well-tested and worked on Windows (XP/Vista/7), Linux (Ubuntu), and Mac OS X (Leopard/Snow Leopard), but let me know if it does not work for you.
The useDistutils option, however, does not work on Windows due to a known issue with recent versions of mingw's gcc (4.4 on, see http://bugs.python.org/issue12641).

I know there are other tools out there (most notably pyximport and sage's misc/cython.py) to do this same kind of thing (and in much cooler ways).
But, Cpyx scratches my itch and I thought I'd put it out there. Happy Cythoning!

P.S.
Unlike pyximport, CythonInline will re-run the code every time it is recompiled. There is some half-finished work towards a cacheing system, but you should really just use pyximport if you want this feature (which is very cool when it works!).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Cpyx-0.1.2.1.tar.gz (7.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page