ImportError in subpackage when running setup.py test

I am trying to create an installation package for a Python project with unit tests enabled. 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 ImportError: No module named core .

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

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

+6
source share
1 answer

You can double check this, but it looks like your tests are importing the disttest package into the tests/ directory, and not into the batch test from the src/ directory.

Why do you need to use a package with the same name as when testing the package? I would just move the testcore module to the test directory or rename the tests/disttest and generally avoid the potential name conflict.

In any case, you want to insert the import pdb; pdb.set_trace() line import pdb; pdb.set_trace() import pdb; pdb.set_trace() just before a failed import and playback using different import statements to see what is being imported from (where import sys; sys.modules['modulename'].__file__ is your friend) so you can better understand that what goes wrong.

+1
source

Source: https://habr.com/ru/post/885899/


All Articles