23

I'm trying to import a cython module data.pyx into another cython module user.pyx. Everything compile fine, but when I try to call user.pyx in a python module, I am getting the error 'ImportError: No module named data'.

Everything is in the same directory.

package/
    __init__.py   #empty
    setup.py      
    data.pxd
    data.pyx
    user.pyx

My setup.py

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


ext_modules = [
    Extension("data", ["data.pyx"]),
    Extension("user", ["user.pyx"],include_dirs = ['myPackageDir'])
    ]

setup(
  name = 'app',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules  
)

Running the following test.py will raised the error.

import user     #this line raised the 'ImportError: No module named data' below
user.doSomething()

The exception I get is

Traceback:
 File "test.py", line 1, in <module>
    import package.user
  File "user.pyx", line 1, in init user (user.c:3384)
ImportError: No module named data

How can I make the import work? Thanks for any help.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • Why did you edit your code to the working code when it should actually demonstrate the wrong code? – Niklas R Mar 30 '12 at 17:21
  • @Niklas I see your point. With my edit, the post becomes confusing. Rob post solves the error 'ImportError: no module named user'. But My actual problem was the error 'ImportError no module named _data_', which I solved by recompiling all my project. As Rob post solves a real potential problem, I marked it as answer, even if it was not my actual problem. Sorry, for the confusion. If there is a better way, please tell me. –  Mar 30 '12 at 19:37

2 Answers2

19

I encounter this problem again in an another project. To solve it, here is what I did:

  • all import and cimport statement must be fully qualified
  • all the python code must be contained in a rootFolder
  • the setup.py must be at the same level than the rootFolder
  • all folder in the rooFolder including the rootFolder must contains a __init__.py
  • in your setup.py the extension's include_dirs must contains '.'

I created a simple project which illustrates this here.
This page helped me created it.
But my project is simpler and I think it would have helped me a lot if I had it.
My project also illustrate how to automatically build all cython files in a project.

Richard Hansen
  • 51,690
  • 20
  • 90
  • 97
2

I might be missing something about Cython, but I think it's:

import package.user
user.doSomething()
rob
  • 2,053
  • 15
  • 13
  • My mistake, in my code I wrote 'package.user' as you suggest. But I forgot to typed it when writing my question. I've corrected my question. –  Mar 27 '12 at 03:26
  • I rebuilt every file in my project and it has fix the problem. Thanks for your help. –  Mar 27 '12 at 14:02
  • You're certainly welcome, but it sounds like you fixed the problem yourself. Glad it's working! – rob Mar 27 '12 at 14:58