|
| 1 | +Pyximport |
| 2 | +========= |
| 3 | + |
| 4 | +Cython is a compiler. Therefore it is natural that people tend to go |
| 5 | +through an edit/compile/test cycle with Cython modules. But my personal |
| 6 | +opinion is that one of the deep insights in Python's implementation is |
| 7 | +that a language can be compiled (Python modules are compiled to .pyc) |
| 8 | +files and hide that compilation process from the end-user so that they |
| 9 | +do not have to worry about it. Pyximport does this for Cython modules. |
| 10 | +For instance if you write a Cython module called ``foo.pyx``, with |
| 11 | +Pyximport you can import it in a regular Python module like this:: |
| 12 | + |
| 13 | + import pyximport; pyximport.install() |
| 14 | + import foo |
| 15 | + |
| 16 | +Doing so will result in the compilation of ``foo.pyx`` (with appropriate |
| 17 | +exceptions if it has an error in it). |
| 18 | + |
| 19 | +If you would always like to import Cython files without building them |
| 20 | +specially, you can also add the first line above to your sitecustomize.py. |
| 21 | +That will install the hook every time you run Python. Then you can use |
| 22 | +Cython modules just with simple import statements. I like to test my |
| 23 | +Cython modules like this:: |
| 24 | + |
| 25 | + python -c "import foo" |
| 26 | + |
| 27 | +See help(pyximport.install) to learn its options for controlling the |
| 28 | +default behavior of ``import`` and ``reload``. |
| 29 | + |
| 30 | +Dependency Handling |
| 31 | +------------------- |
| 32 | + |
| 33 | +In Pyximport 1.1 it is possible to declare that your module depends on |
| 34 | +multiple files, (likely ``.h`` and ``.pxd`` files). If your Cython module is |
| 35 | +named ``foo`` and thus has the filename ``foo.pyx`` then you should make |
| 36 | +another file in the same directory called ``foo.pyxdep``. The |
| 37 | +``modname.pyxdep`` file can be a list of filenames or ``globs`` (like |
| 38 | +``*.pxd`` or ``include/*.h``). Each filename or glob must be on a separate |
| 39 | +line. Pyximport will check the file date for each of those files before |
| 40 | +deciding whether to rebuild the module. In order to keep track of the |
| 41 | +fact that the dependency has been handled, Pyximport updates the |
| 42 | +modification time of your ``.pyx`` source file. Future versions may do |
| 43 | +something more sophisticated like informing distutils of the |
| 44 | +dependencies directly. |
| 45 | + |
| 46 | +Limitations |
| 47 | +----------- |
| 48 | +Pyximport does not give you any control over how your Cython file is |
| 49 | +compiled. Usually the defaults are fine. You might run into problems if |
| 50 | +you wanted to write your program in half-C, half-Cython and build them |
| 51 | +into a single library. Pyximport 1.2 will probably do this. |
| 52 | + |
| 53 | +Pyximport does not hide the Distutils/GCC warnings and errors generated |
| 54 | +by the import process. Arguably this will give you better feedback if |
| 55 | +something went wrong and why. And if nothing went wrong it will give you |
| 56 | +the warm fuzzy that pyximport really did rebuild your module as it was |
| 57 | +supposed to. |
| 58 | + |
| 59 | +For further thought and discussion |
| 60 | +---------------------------------- |
| 61 | + |
| 62 | +``setup.py install`` does not modify ``sitecustomize.py`` for you. Should it? |
| 63 | +Modifying Python's "standard interpreter" behaviour may be more than |
| 64 | +most people expect of a package they install.. |
| 65 | + |
| 66 | +Pyximport puts your ``.c`` file beside your ``.pyx`` file (analogous to |
| 67 | +``.pyc`` beside ``.py``). But it puts the platform-specific binary in a |
| 68 | +build directory as per normal for Distutils. If I could wave a magic |
| 69 | +wand and get Cython or distutils or whoever to put the build directory I |
| 70 | +might do it but not necessarily: having it at the top level is VERY |
| 71 | +HELPFUL for debugging Cython problems. |
0 commit comments