Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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