2

Following these examples (https://github.com/cython/cython/wiki/PackageHierarchy , https://groups.google.com/forum/#!msg/cython-users/6trL0V1bLx4/7bxhj0xCK50J and Cython: ImportError: No module named 'myModule': how to call a cython module containing a cimport to another cython nodule?), I have created the following cython package setup:

test_outside.py
cython_pckg/
  __init__.py
  setup.py
  test_inside.py
  Code/
    __init__.py
    worker/
      __init__.py
      worker_1.pyx
      worker_1.pxd
      worker_2.pyx
      worker_2.pxd
      worker_3.pyx
      worker_3.pxd

My setup.py file looks as follows:

from distutils.core import setup
from distutils.extension import Extension
import numpy
from Cython.Distutils import build_ext

ext_modules = [
   Extension("Code.worker.worker_1", ["Code/worker/worker_1.pyx"], include_dirs=[".", numpy.get_include()]),
   Extension("Code.worker.worker_2", ["Code/worker/worker_2.pyx"], include_dirs=["."]),
   Extension("Code.worker.worker_3", ["Code/worker/worker_3.pyx"], include_dirs=[".","./Code/worker/", numpy.get_include()])
]

setup(name="C_Extensions",
  cmdclass={"build_ext": build_ext},
  ext_modules=ext_modules,
  script_args=["build_ext"],
  options={'build_ext':{'inplace':True, 'force':True}}
)

Please note, that worker_3 imports worker_2. If I try to import these modules in test_inside.py, everything works fine. However, importing the modules into test_outside.py throws an ImportError: No module named Code.worker.worker_2 for the file worker_3.pyx. On the top level __init__.py, I import everything.

from Code.worker.worker_1 import *
from Code.worker.worker_2 import *
from Code.worker.worker_3 import *

How can I make this work?

swot
  • 278
  • 1
  • 3
  • 10

1 Answers1

1

In worker_3.pyx:

cimport worker_2

Note - not cimport Code.<..>

Cython only looks at available .pxd files when cimport is used.

It has no knowledge of module level name spacing, meaning the module name defined in setup.py's Extension.

Reference - Cython docs.

danny
  • 5,140
  • 1
  • 19
  • 31
  • It seems this does not work for me. I still get the same error. – swot Sep 13 '17 at 13:20
  • Has everything been recompiled? You cannot be getting `ImportError: No module named Code.worker.worker_2` if not using `cimport Code.worker.worker_2` – danny Sep 13 '17 at 13:28
  • Yes. The problem still exists. I can solve the problem by moving the `setup.py` file up one level. But what is happening then? – swot Sep 13 '17 at 13:35
  • I do not understand what that means. How can you have a setup.py outside the package dir. You'll have to show a complete, reproducible and verifiable example of how to reproduce the error including relevant code for anything further. – danny Sep 13 '17 at 13:40
  • I'd also remove the `import *` from the top `__init__.py`. It should be using relative imports firstly and `import *` is considered bad practice secondly. – danny Sep 13 '17 at 13:50
  • 2
    Right, that's a different issue. The name of your package (what is used in python to `import` from) is not `Cythongist`, it is `code` as that is the package being installed by [setup.py](https://github.com/erathorn/Gist_cython/blob/master/Cythongist/setup.py#L15). Which is why it 'worked' when setup.py was moved up a level. In short, fix the setup.py first, make sure regular python modules can be imported from it, then the cython stuff will just work with the answer above. See [setuptools docs](https://pythonhosted.org/an_example_pypi_project/setuptools.html). – danny Sep 13 '17 at 14:51