Nosetests gives ImportError when __init__.py is included (using cython)

143 views Asked by At

I just came across a very strange error while using nose and cython inside my virtualenv with python3. For some reason nosetests started giving me an ImportError even though python -m unittest basic_test.py was working. I made a new directory to reproduce the error to make sure there wasn't something weird in that directory.

Here are the three files: fileA.pyx, setup.py, and basic_test.py

file1.pyx
class FileA:
    def __init__(self):
        self.temp = {}

setup.py:

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

ext_modules = [
    Extension('fileA', ['fileA.pyx'],)
]
setup(
    name='test',
    ext_modules=ext_modules,
    cmdclass={'build_ext': build_ext},
)

basic_test.py:

def test():
        from fileA import FileA
        FileA()
        assert True

Fresh directory. I run python setup.py build_ext --inplace. It compiles. I run nosetests the single test passes.

Then I do touch __init__.py and then run nosetests again and it fails with this error:

ImportError: No module named 'fileA'

Is this a bug or do I not understand how init affects imports?

Update: I found this post about import traps and read something that might explain how adding init breaks it. I still don't get exactly how it fits in though.

This is an all new trap added in Python 3.3 as a consequence of fixing the previous trap: if a subdirectory encountered on sys.path as part of a package import contains an init.py file, then the Python interpreter will create a single directory package containing only modules from that directory, rather than finding all appropriately named subdirectories as described in the previous section.

0

There are 0 answers