Python cython
Documentation
http://docs.cython.org/en/latest/ official docs https://github.com/cython/cython/wiki/CythonExtensionsOnWindows official docs for windows https://wiki.python.org/moin/WindowsCompilers windows compilers for each version of python https://docs.microsoft.com/en-us/visualstudio/python/working-with-c-cpp-python-in-visual-studio MSDN documentation for c/cpp with python https://docs.python.org/3/extending/windows.html official python docs on compiling extensions https://dimitri.janczak.net/2017/05/20/python-3-6-visual-studio-2017/ blog post on compiling on windows for python 3.6
Install
The method you choose to install cython depends on what you are compiling for.
- cython must be built using same compiler used to build your python version
- compiling cythonized software must also use the same compiler used to build your python version
system python
If you are targeting a normal python interpreter, on an officially supported platform, you can install a wheel.
pip install cythonembedded python
If you are targeting an embedded python, you'll need to build it from source.
build cython from source
1. Download both setuptools and cython source code from pypi.
2. Extract/Install setuptools
tar -xvf setuptools-X.X.X.tar.gz cd setuptools your-python setup.py install3. Setup shell environment for compiler used to build your embedded python (if necessary on your platform)
vcvarsall.bat amd86_64
4. Extract/Build/Install cython
tar -xvf cython-x.x.x.tar.gz cd cython your-python setup.py install
Usage
Cythonizing python source code requires two steps.
- convert your python code to C sourcecode (cythonize)
- compile cythonized sourcefiles
cythonize
Cythonizing converts your python code into C code. This can be done from the commandline, or from code.
commandline
cythonize file.py # > file.cpython
from Cython import Build c_srcfiles = Build.cythonize('your/file.py') # writes your/file.cbuild
See C compilers to choose an appropriate compiler.
setup.py method
#### setup.py from setuptools import setup from Cython import Build setup( packages=setuptools.find_packages(), script_args=['build_ext', '-b', '/your/desired/build/dir'], # commandline args ext_modules=Build.cythonize( [ setuptools.Extension('tmalibqt.icons.icons', ['/tmarepo/src/tmalibqt/icons/icons.py']), setuptools.Extension('tmalibqt.concurrency.workers', ['/tmarepo/src/tmalibqt/concurrency/workers.py']), ], compiler_directives={ 'language_level': str(sys.version_info[0]), # which language should be used for .pyc stub files 'always_allow_keywords': True, # args can be assigned like keyword func(arg='blah') 'profile': True, # allows cProfile in compiled code } ), )then build from commandline
python setup.py build_ext # build pyd files python setup.py bdist_egg # build eggfileNOTE:
The produced .pyd/.so files will also have stub .pyc files that are required to load it.
If you are packaging these into an eggfile, information about the python extensions must be recorded into the EGG_INFO/ directory. setuptools will handle this for you automatically, if you let it.
See here for a full list of compiler directives: http://docs.cython.org/en/latest/src/userguide/source_files_and_compilation.html#compiler-directives
build standalone executable
WARNING:
I haven't tested this
from Cython.Build import BuildExecutable BuildExecutable.build('/your/python/file.py')limitations
- cython keeps track of it's incompatibilities with C-python:
__init__.py
does not compile on windows, you'll need to keep these in .py/.pyx- cython removes some python features for speed optimizations. you may turn these back on. See: