I would like to import the package cythonAnimal into a module located
in the package pythonCaller.
But I keep getting an import error.
My directory is as follow:
sampleProject/
cythonAnimal/
__init__.py
cat.pxd
cat.pyx
dog.pxd
dog.pyx
setup.py
pythonCaller/
__init__.py
outOfPackageCall.py
outOfPackageCall.py:
from cythonAnimal.cat import Cat #Fail here with the error below
from cythonAnimal.dog import Dog
cat = Cat()
dog = Dog()
print
cat.name() + ' is friend with ' + cat.friendWith() #should
print 'cat is friend with dog'
print
dog.name() + ' is friend with ' + dog.friendWith() #should
print 'dog is friend with cat'
The error is:
Traceback (most recent call last):
File "/sampleProject/pythonCaller/outOfPackageCall.py", line 1,
in <module>
from cythonAnimal.cat import Cat
File "dog.pxd", line 3, in init cat (cat.c:962)
ImportError: No module named dog
The faulty line in question (cat.c:962) is:
__pyx_ptype_3dog_Dog = __Pyx_ImportType("dog", "Dog",
sizeof(struct __pyx_obj_3dog_Dog), 1); if (unlikely(!
__pyx_ptype_3dog_Dog)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 3;
__pyx_clineno = __LINE__; goto __pyx_L1_error;}
My other file are as follows:
cat.pxd:
from dog cimport Dog
cdef class Cat:
cpdef name(self)
cpdef friendWith(self)
cat.pyx:
cdef class Cat:
cpdef name(self):
return 'cat'
cpdef friendWith(self):
cdef Dog dog = Dog()
return
dog.name()
dog.pxd:
from cat cimport Cat
cdef class Dog:
cpdef name(self)
cpdef friendWith(self)
dog.pyx:
cdef class Dog:
cpdef name(self):
return 'dog'
cpdef friendWith(self):
cdef Cat cat = Cat()
return
cat.name()
setup.py:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import os
packageDir = os.path.dirname(__file__)
includedDir = [packageDir]
os.chdir(packageDir)
ext_modules = [
Extension("cat", ["cat.pyx"], include_dirs=includedDir),
Extension("dog", ["dog.pyx"], include_dirs=includedDir)
]
setup(
name='cythonAnimal',
cmdclass={'build_ext': build_ext},
include_dirs=includedDir,
ext_modules=ext_modules,
script_args=['build_ext'],
options={'build_ext':{'inplace':True, 'force':True}}
) #I run my setup.py directly from the interpreter with:
reload(cythonAnimal.setup)