Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 671
Henrik Heimbuerger Avatar asked Apr 14 '11 10:04

Henrik Heimbuerger


1 Answers

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.

like image 179
Martijn Pieters Avatar answered Sep 17 '22 00:09

Martijn Pieters