About Cython
Cython is an optimising static compiler for both the
Python programming
language and the extended Cython programming language (based on Pyrex).
It makes writing C extensions for Python as easy as Python itself.
Cython gives you the combined power of Python and C to let you
The Cython language is a superset of the Python language that
additionally supports calling C functions and declaring
C types on variables and class attributes. This allows the
compiler to generate very efficient C code from Cython code.
The C code is generated once and then compiles with all major
C/C++ compilers in CPython 2.7 as well as 3.5 and all later versions.
The upcoming Cython 3.1 requires Python 3.8 or later.
We regularly run integration tests against all supported CPython versions and
their latest in-development branches to make sure that the generated code stays
widely compatible and well adapted to each version.
PyPy support is mostly usable.
The latest PyPy version is always recommended here.
Support for the CPython Limited API and free-threading CPython is available
in Cython 3.1 but considered experimental.
All of this makes Cython the ideal language for wrapping
external C libraries, embedding CPython into existing
applications, and for fast C modules that speed up the
execution of Python code.
🌷️ Your donation can help the Cython project! 🌷️️
Making Cython a great programming language for you,
and keeping it up to speed with the Python ecosystem and the changing requirements
of its diverse user bases, takes a lot of time and dedication.
To support the maintenance and
future development of the Cython language and compiler,
YOU can sponsor the work of Stefan Behnel via:
Note that PayPal takes 5 - 15% fees for small non-EUR payments,
which is money that you pay without helping us.
Consider signing up for a GitHub Sponsors subscription instead.
Documentation
External resources:
If you still have questions, feel free to send an email to the
cython users mailing list.
Aspects of the core development are discussed on the
cython core developer
mailing list. If you are unsure which list to use, the cython users list is probably the right one to use, which has the larger audience.
There is also a #cython channel on the freenode IRC servers for Cython related chats.
Download
Cython is freely available under the open source Apache License.
The latest stable release of Cython is 3.0.11 (released 2024-08-05).
Cython is available from the PyPI package index repository.
People
Core Developers:
Stefan Behnel,
Robert Bradshaw,
David Woods,
Matúš Valo,
Lisandro DalcĂn
Contributors:
Marc Abramowitz,
Wichert Akkerman,
Martin Albrecht,
Peter Alexander,
Francesc Alted,
Ivan Andrus,
Arfrever Frehtes Taifersar Arahesis,
Philip Austin,
Josh Ayers,
Haoyu Bai,
Grant Baillie,
Nicolas Barbey,
Gustavo Barbieri,
David Barnett,
Andrea Bedini,
Brian Bishop,
Chuck Blake,
Alexey Borzenkov,
Georg Brandl,
Nils Braun,
Erik Bray,
Matthew Brett,
Nils Bruin,
Matthias Bussonnier,
Lars Buitinck,
Vladimir Cerny,
Ondrej Certik,
Shalabh Chaturvedi,
Eric Chlebek,
David Christenson,
Craig Citro,
Timothy Clemans,
Bryan Cole,
Favian Contreras,
Dave Cournapeau,
Andreas van Cranenburgh,
Antonio Cuni,
Armon Dadgar,
Julien Danjou,
Jeroen Demeyer,
Eric Dill,
Nicolas Dumazet,
Ali Ebrahim,
John Ehresman,
Michael EnĂźlin,
Jason Evans,
Isuru Fernando,
Eric Firing,
Mark Florisson,
Claudio Freire,
Danilo Freitas,
Bradley Froehle,
Gary Furnish,
Syam Gadde,
MartĂn Gaitán,
Gabriel Gellner,
Christoph Gohlke,
Evgeny Golyshev,
Brian Granger,
Olivier Grisel,
Christoph Groth,
Jason Grout,
Romain Guillebert,
Adrien Guinet,
Valentin Haenel,
Yaroslav Halchenko,
Richard Hansen,
Kay Hayen,
Ian Henriksen,
Philip Herron,
Magnus Lie Hetland,
David Hirschfeld,
Jiajun Huang,
Thomas Hunger,
Eric Huss,
Naoki Inada,
Joe Jevnik,
Omer Katz,
Karl Kempe,
Rafe Kettler,
Jerome Kieffer,
W. Trevor King,
Jim Kleckner,
Ronan Lamy,
Torsten Landschoff,
Chris Lasher,
Sergei Lebedev,
Antony Lee,
Björn Linse,
Mark Lodato,
Thomas Lotze,
Vitja Makarov,
Angus McMorland,
David McNab,
Jason Madden,
Syrtis Major,
Yesudeep Mangalapilly,
Tadeu Manoel,
Vasil Manolov,
Gabriel de Marmiesse,
Sturla Molden,
Mansour Moufid,
Nikita Nemkin,
David Nogueira,
Andrew Ohana,
Jay Oster,
Olivier Parcollet,
Brent Pedersen,
Mark Peek,
Chris Perkins,
Gabriel Pettier,
Emmanuel Gil Peyrot,
Matti Picus,
Antoine Pitrou,
Andrey Plotnikov,
Paul Prescod,
Prabhu Ramachandran,
Jeff Ramnani,
Holger Rapp,
Nikolaus Rath,
Peinthor Rene,
Armin Rigo,
Joon Ro,
Fabian Rost,
Ralf Schmitt,
Michael Seifert,
Dag Sverre Seljebotn.
Corbin Simpson,
Kirill Smelkov,
Ivan Smirnov,
Kurt Smith,
Nathaniel Smith,
William Stein,
Andrew Straw,
Boxiang Sun,
Arfrever Taifersar Arahesis,
Dimitri Tcaciuc,
Gregor Thalhammer,
Kevin R. Thornton,
Peter Todd,
Erik Tollerud,
David Vierra,
Petr Viktorin,
Mathieu Virbel,
Pauli Virtanen,
Jakub Wilk,
Carl Witty,
Felix Wu,
Yury Zaytsev,
Jelle Zijlstra.
Financial Contributions:
Google and Enthought funded Dag Seljebotn to greatly improve
Cython integration with NumPy.
Kurt Smith and Danilo Freitas were funded through the Google Summer of Code program to work on improved Fortran and C++ support respectively,
and in 2010 Haoyu Bai was funded to work on Python 3 compatibility.
Special Thanks
to Greg Ewing for inventing and developing Cython's predecessor
Pyrex
and for his valuable input in language design decisions.
Useful Links
What users have to say about Cython:
»You would expect a whole lot of organizations and people to fancy a
language that's about as high-level as Python, yet almost as fast and
down-to-the-metal as C.
Add to that the ability to seamlessly integrate with both
your existing C/++ codebase and your Python codebase, easily mix very
high level abstractions with very low-level machine access... clear
winner.« →
Dun Peal on c.l.py
»You guys rock!
In scikit-learn, we have decided early on to do Cython, rather than C or
C++. That decision has been a clear win because the code is way more
maintainable. We have had to convince new contributors that Cython was
better for them, but the readability of the code, and the capacity to
support multiple Python versions, was worth it.« →
Gaël Varoquaux
»The biggest surprise (and of course this is Cython's selling
point) is how simple the interfacing between high level and low level
code becomes, and the fact that it is all very robust.
It's exiciting to see that there are several active projects around
that attempt to speed up Python. The nice thing about Cython is that
it doesn't give you "half the speed of C" or "maybe nearly the speed
of C, 3 years from now" -- it gives the real deal, -O3 C, and it works
right now.« →
Fredrik Johansson
»SciPy is approximately 50% Python, 25% Fortran, 20% C, 3% Cython
and 2% C++ … The distribution of secondary programming languages in SciPy
is a compromise between a powerful, performance-enhancing language that
interacts well with Python (that is, Cython) and the usage of languages
(and their libraries) that have proven reliable and performant over many
decades.
For implementing new functionality, Python is still the language
of choice. If Python performance is an issue, then we prefer the use of
Cython followed by C, C++ or Fortran (in that order). The main motivation
for this is maintainability: Cython has the highest abstraction level, and
most Python developers will understand it. C is also widely known, and
easier for the current core development team to manage than C++ and
especially Fortran.« →
Pauli Virtanen et al., SciPy
»Not to mention that the generated C often makes use of
performance tricks that are too tedious or arcane to write by hand,
partially motivated by scientific computing’s constant push. And
through all that, Cython code maintains a high level of integration
with Python itself, right down to the stack trace and line numbers.
PayPal has certainly benefitted from their efforts through
high-performance Cython users like gevent, lxml, and NumPy. While our
first go with Cython didn’t stick in 2011, since 2015, all native
extensions have been written and rewritten to use Cython.«
→
Mahmoud Hashemi
»Cython produces binaries much like C++, Go, and Rust do. Now with GitHub Actions the
cross-platform build and release process can be automated for free for Open Source projects.
This is an enormous opportunity to make the Python ecosystem 20-50% faster with a single
pull request.«
→
Grant Jenks
»I'm honestly never going back to writing C again. Cython gives
me all the expressiveness of Python combined with all the performance
and close-to-the-metal-godlike-powers of C. I've been using it to
implement high-performance graph traversal and routing algorithms and
to interface with C/C++ libraries, and it's been an absolute amazing
productivity boost.« →
Andrew Tipton
»A general rule of thumb is that your program spends 80% of
its time running 20% of the code. Thus a good strategy for efficient
coding is to write everything, profile your code, and optimize the
parts that need it. Python’s profilers are great, and Cython allows
you to do the latter step with minimal effort.« →
Hoyt Koepke
»The question was, in auto-generated code, to what extent there
were bugs there, to what extent there were bugs in the generators. The
first time I did this, I got lots and lots of warnings from the tool for
code generated by both SWIG and Cython [...]
Basically, everything I found Cython emitting was a false positive and
a bug in my checker tool [CPyChecker].« →
David Malcolm
»Basically, Cython is about 7x times faster than Boost.Python, which
astonished me.« →
Chris
Chou
»Using Cython allows you to just put effort into speeding up the
parts of code you need to work on, and to do so without having to
change very much. This is vastly different from ditching all the code
and reimplementing it another language. It also requires you to learn
a pretty minimal amount of stuff. You also get to keep the niceness of
the Python syntax which may Python coders have come to
appreciate.« →
Craig Macomber
»If you have a piece of Python that you need to run fast, then I
would recommend you used Cython immediately. This means that I can
exploit the beauty of Python and the speed of C together, and that’s a
match made in heaven.« →
Stavros
»From 85 seconds (at the beginning of this post) down to 0.8
seconds: a reduction by a factor of 100 ...thank you cython!
:-)« →
André Roberge
»Writing a full-on CPython module from scratch would probably
offer better performance than Cython if you know the quirks and are
disciplined. But to someone who doesn't already drip CPython C
modules, Cython is a godsend.
Ultimately, there's 5 commonly used ways (CPython [C-API],
Boost::Python, SWIG, Cython, ctypes) to integrate C into Python, and
right now you'd be crazy not to give Cython a shot, if that's your
need. It's very easy to learn for anyone familiar with both C and
Python.« →
ashika
»What I loved about the Cython code is that I use a Python
list to manage the Vortex objects. This shows that we can use the
normal Python containers to manage objects. This is extremely
convenient. [...]
Clearly, if you are building code from scratch and need speed,
Cython is an excellent option. For this I really must congratulate the
Cython and Pyrex developers.« →
Prabhu Ramachandran
»I wrote a script that compute a distance matrix (O^2) in
Python with Numpy arrays and the same script in Cython. It took me 10
minutes to figure it out how Cython works and I gained a speed up of
550 times !!! Amazing« →
kfrancoi
»I would like to report on a successful Cython project.
Successful in the sense that it was much faster than all code written
by my predecessors mainly because the speed scales almost linearly
with the number of cores. Also, the code is shorter and much easier
to read and maintain. [...]
Making it this fast & short & readable & maintainable
would have been pretty hard without Cython.« →
Alex van Houten
»At work, we’ve started using Cython with excellent success.
We rewrote one particular Perl script as Cython and achieved a 600%
speed improvement. As a Perl lover, this was impressive. We still
get all the benefits of Python such as rapid development and clean
object-oriented design patterns but with the speed of C.« →
Wim Kerkhoff
»The reason that I was interested in Cython was the long
calculation times I encountered while doing a multi-variable
optimization with a function evaluation that involved solving a
differential equation with scipy.integrate.odeint. By simply
replacing the class that contained the differential equation with a
Cython version the calculation time dropped by a factor 5. Not bad
for half a Sunday afternoons work.« →
Korbinin
»I was surprised how simple it was to get it working both
under Windows and Linux. I did not have to mess with make files or
configure the compiles. Cython integrated well with NumPy and SciPy.
This expands the programming tasks you can do with Python
substantially.« →
Sami Badawi
»This is why the Scipy folks keep harping about Cython – it’s
rapidly becoming (or has already become) the lingua franca of exposing
legacy libraries to Python. Their user base has tons of legacy code
or external libraries that they need to interface, and most of the
reason Python has had such a great adoption curve in that space is
because Numpy has made the data portion of that interface easy.
Cython makes the code portion quite painless, as well.« →
Peter Z. Wang
»Added an optional step of compiling fastavro with Cython.
Just doing that, with no Cython specific code reduced the time of
processing 10K records from 2.9sec to 1.7sec. Not bad for that little
work.« →
Miki Tebeka
»fastavro compiles the Python code without any specific
Cython code. This way on machines that do not have a compiler users
can still use fastavro.
The end result is a package that reads Avro faster than Java
and supports both Python 2 and Python 3. Using Cython and a little bit
of work th[is] was achieved without too much effort.« →
Miki Tebeka
»... the binding needed to be rewritten, mainly because the
current binding is directly written in C++ and is a maintenance
nightmare. This new binding is written in Cython« →
Bastien LĂ©onard
»
Code generation via Cython allows the production of smaller and more maintainable bindings, including increased compatibility with all supported Python releases without additional burden for NEST developers.
«
This approach resulted in a reduction of the code footprint of around 50% and a significant increase in the cohesiveness of the code related to the Python bindings: whereas previously seven core files and 22 additional files were involved, the new approach requires merely two core files. The new implementation also removes the compile-time dependency on NumPy and provides numerous additional maintainability benefits by reducing complexity and increasing comprehensibility of the code. The re-write of the build system also resulted in a 50% reduction of code, and resolved multiple issues with its usability and robustness.
«
»
In conclusion, we hope that through a more widespread use of Cython, neuroscientific software developers will be able to focus their creative energy on refining their algorithms and implementing new features, instead of working to pay off the interest on the accumulating technical debt.
« →
Yury V. Zaytsev and Abigail Morrison
»
The Cython version took about 30 minutes to write, and it runs just as fast as the C code — because, why wouldn’t it? It *is* C code, really, with just some syntactic sugar. And you don’t even have to learn or think about a foreign, complicated C API…You just, write C. Or C++ — although that’s a little more awkward. Both the Cython version and the C version are about 70x faster than the pure Python version, which uses Numpy arrays.
« →
Matthew Honnibal
»
I love this project. Fantastic way to write Python bindings for native libs or speed up computationally intensive code without having to write C yourself.
« →
schmichael
»
I use a lot of pyrex/cython to bind to libraries - it's so much faster to code in python. It's been a huge boon. Having used swig, hand writing wrappers, and pyrex before i can say i much prefer cython.
Thank you for the hard work.
« →
jnazario
»
I am not good with C so I mostly do pure python for my research. However, now dealing with clusters of 1000+ molecules, there was huge bottlenecks in my code.
Using cython it went from running single calculation in hours to seconds, focking nice...
« →
fishtickler
»
Cython saves you from a great many of the gotchas [that C has].
The worst you'll usually get is a lack of performance gain (at which point cython -a is your friend).
Wringing out all the performance you can get can require a reasonable working knowledge of C -- but you don't have to know it that well to do pretty darn well.
« →
lmcinnes
»
[spaCy is] written in clean but efficient Cython code, which allows us
to manage both low level details and the high-level Python API in a
single codebase.
« →
Matthew Honnibal
»
[uvloop] is written in Cython, and by the way, Cython is just amazing.
It's unfortunate that it's not as wide-spread and I think it's kind-a
underappreciated what you can do in Cython. Essentially, it's a
superset of the Python language, you can strictly type it and it will
compile to C and you will have C speed. You can easily achieve it,
with a syntax more similar to Python. Definitely check out Cython.
« →
Yury Selivanov
(video@22:50)
»
300.000 req/sec is a number comparable to Go's built-in web server
(I'm saying this based on a rough test I made some years ago).
Given that Go is designed to do exactly that, this is really impressive.
My kudos to your choice to use Cython.
« →
beertown
»
Cython is one of the best kept secrets of Python. It extends Python
in a direction that addresses many of the shortcomings of the language
and the platform
« →
UlaĹź TĂĽrkmen
more ...less ...