Henrik Heimbuerger
Henrik Heimbuerger

Reputation: 10193

ImportError on subpackage when running setup.py test

I'm trying to create an install package for a Python project with included unit tests. My project layout is as follows:

setup.py
src/
    disttest/
        __init__.py
        core.py
tests/
    disttest/
        __init__.py
        testcore.py

My setup.py looks like this:

from distutils.core import setup
import setuptools

setup(name='disttest',
      version='0.1',
      package_dir={'': 'src'},
      packages=setuptools.find_packages('src'),
      test_suite='nose.collector',
      tests_require=['Nose'],
      )

The file tests/disttest/testcore.py contains the line from disttest.core import DistTestCore.

Running setup.py test now gives an ImportError: No module named core.

After a setup.py install, python -c "from disttest.core import DistTestCore" works fine. It also works if I put import core into src/disttest/__init__.py, but I don't really want to maintain that and it only seems necessary for the tests.

Why is that? And what is the correct way to fix it?

Upvotes: 6

Views: 1311

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121864

You may want to double-check this, but it looks like your tests are importing the disttest package in the tests/ directory, instead of the package-under-test from the src/ directory.

Why do you need to use a package with the same name as the package-under-test? I'd simply move the testcore module up to the tests directory, or rename the tests/disttest package and avoid the potential naming conflict altogether.

In any case, you want to insert a import pdb; pdb.set_trace() line just before the failing import and play around with different import statements to see what is being imported from where (import sys; sys.modules['modulename'].__file__ is your friend) so you get a better insight into what is going wrong.

Upvotes: 2

Related Questions