Converting python code to cython
Question: I have a python program that uses OpenCV. The program runs as expected as it
is at the moment. Now I would like to use Cython to compile my python code to
C code. I am doing this instead of re-writing the entire program in C because
I would still like other python programs to be able to `import my_program`.

I have never used Cython before but have just read few blog posts about it.
Can someone please tell me what I should be prepared for and how much of an
uphill task it would be.... My current python program is ~200 LoC.


Answer: Based on your comments you're looking to run your existing code "as is" to
avoid providing the source, rather than make any significant changes to use
Cython-specific features. With that in mind I'd expect it to just work without
any major effort. One easy alternative to consider would be to just provide
pyc bytecode files.

A list of minor gotchas that I know of (in rough order of importance) follows.
A few others are listed [in the
documentation](http://docs.cython.org/src/userguide/limitations.html). Most of
these are fairly minor so you'd be unlucky to meet them.

  1. You will likely have to recompile your module for every platform, 32bit and 64bit, every (major, e.g. 3.4, 3.5) version of Python used, and possibly on Windows with multiple different compilers. 

  2. You can't use `__file__` at the module level. This is sometimes becomes an issue when trying to find the path of static resources stored in the same place as your code.

  3. A few things that try to do clever things by inspecting the stack (to see what variables are defined in the functions that called them) break, for example [some of sympy](http://stackoverflow.com/questions/36191146/lambdify-works-with-python-but-throws-an-exception-with-cython/36199057) and possibly some shortcuts to string formatting ([see for example](http://stackoverflow.com/questions/13312240/is-a-string-formatter-that-pulls-variables-from-its-calling-scope-bad-practice) for some recipes that might use this idea)

  4. Anything that looks at the bytecode of functions (since it isn't generated by Cython). Numba is probably the most commonly used example in numerical python, but I know of at least one (unmaintained) [MATLAB/Python wrapper](http://ompc.juricap.com/) that inspects the bytecode of the calling function to try to work out the number of arguments being returned.

  5. [You must have an `__init__.py` file to make a folder into a module - it won't recognise a compiled `__init__.so` file on its own](http://stackoverflow.com/questions/28261147/cython-package-with-init-pyx-possible/32067984#32067984).

  6. [String concatenation can go through a fast path in Python that Cython doesn't manage](http://stackoverflow.com/questions/35787022/cython-string-concatenation-is-super-slow-what-else-does-it-do-poorly). You should not being doing this too much in your code anyway, but you may see large performance differences if you rely on it. 

