[Pyrex] cimport

William Stein was at math.harvard.edu
Sat Mar 5 08:46:07 CET 2005


Hello,

I'm creating a large amount of code that uses Pyrex.  I have to cimport files 
from one package into another.   In order to use cimport across directories 
and have everything work correctly, I've noticed that it's necessary to use 
exact notation, e.g., 

cimport sage.rings.rational
import sage.rings.rational

instead of

cimport rational
import rational

Here, we *must* make a symlink, i.e., 
   ln -s rational sage.rings.rational
in the directory that contains rational, otherwise the cimport above will 
fail.  

However, the above doesn't work if in a given file, we must include more than 
one such package from another directory, since there seems to be a a bug in 
pyrex.  When including more than one cimport from another directory, pyrexc 
complains about duplicate declarations. 

Fortunately, I was able to program around it as follows.

I comment out lines 568-571 of Pyrex/Compiler/Symtab.py, i.e., 
  the "if entry:" part of the declare_module function.

With this commented out, pyrex code with multiple exact path cimport's will 
compile and run through gcc fine, and the resulting code passes my unit 
tests.

My question is whether I should be doing this all differently, and if not, 
what are the plans for how to support cimport across directories.  I'm a huge 
fan of Pyrex, so I really hope it will provide even more support for large 
projects.  

 -- William

P.S. Greg, the last two bullets at 
http://nz.cosc.canterbury.ac.nz/~greg/python/Pyrex/version/Doc/sharing.html
contain typos.  In "There is a cdef class shrubbery", the shrubbery should be 
capitalized, and in "In Landscaping.pyx, the cimport Shrubbery declaration" 
the "Shrubbery" should be "Shrubbing". 



More information about the Pyrex mailing list