On Thu, May 26, 2016 at 12:30 PM, Paul Londino <
lond...@gmail.com> wrote:
> Sorry, I think the title I wrote was misleading. I really want to separate
> out from the pure python code (but keep together) all of the .pyx and .pxd
> into a subfolder (either as part of a subpackage or even outside the package
> in a "cython_files" folder or something like that) but then have the module
> built by Cython to be in the same package.
>
> So like for pure Python, where the folder structure on disk doesn't have to
> mimic the package layout,
In pure Python, the folder structure on the disk does mimic (in fact
it determines) the package layout (along with the __init__.py files).
The same is true for Cython.
> I want to have packages like 'package_a.cy_module'
> even though the source (pxd and pyx files) love in a separate directory like
> 'package_a/cython_files/cymodule.pyx'.
>
> I had this working fine until I wanted to import something into a .pyx file
> from the .pxd of another Cython module, but something about how the imports
> work seems to prevent it being able to find what's imported from the other
> Cython module. I tried:
>
> from package_a.cython_files cimport rect
>
> which results in the runtime import error and
>
> from package_a cimport rect
>
> which results in a Cython compile error (about not being able to find the
> .pxd)
>
> So I'm curious if this should be possible and I'm just missing something, or
> if it's not possible.
This is not possible as cimport must do both a static import and a
runtime import against the same path.
If you want your cython modules next to your python modules, put your
.pyx files next to your .py files. Alternatively you can put them in a
subpackage (and import them from this subpackage). If you really want,
you could create .py files that import * from your .pyx files that
live in a subpackage, but that's just extra overhead and cimports
would still have to come from the proper subpackage.
It's a feature, not a bug, that you can put .py and .pyx files in the
same directory. This also means your users won't have to take any
action (or even know) if you decide to cythonize (or uncythonize) a
given module.
Your setup.py listing a module as "package_a.cy_module" while its
source is "package_a/cython_files/cy_module.pyx" is causing the
discrepancy between imports and cimports here--being consistent will
save you a lot of headaches. (I take it back, it might be possible if
you manually muck around with sys.modules and import hooks to hide the
fact that these modules are in a subpackage, but I wouldn't advise
that).