3

I am trying to test my application against multiple variants of Python on Travis CI. In Python 2.7, the tests pass as expected. However, in Python 3.4 I get this traceback

ERROR: tests.test_mymodule (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/python/3.4.2/lib/python3.4/unittest/case.py", line 58, in testPartExecutor
    yield
  File "/opt/python/3.4.2/lib/python3.4/unittest/case.py", line 577, in run
    testMethod()
  File "/opt/python/3.4.2/lib/python3.4/unittest/loader.py", line 32, in testFailure
    raise exception
ImportError: Failed to import test module: tests.test_mymodule
Traceback (most recent call last):
  File "/opt/python/3.4.2/lib/python3.4/unittest/loader.py", line 312, in _find_tests
    module = self._get_module_from_name(name)
  File "/opt/python/3.4.2/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
    __import__(name)
  File "/home/travis/build/MyGitHubName/mymodule/tests/test_mymodule.py", line 8, in <module>
    from mymodule import mymodule
  File "/home/travis/build/MyGitHubName/mymodule/mymodule/__init__.py", line 1, in <module>
    from mymodule import MyModule
ImportError: cannot import name 'MyModule'

In Python 3.5 I get this (similar, but not exactly the same as 3.4)

ERROR: mymodule (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: mymodule
Traceback (most recent call last):
  File "/opt/python/3.5.0/lib/python3.5/unittest/loader.py", line 462, in _find_test_path
    package = self._get_module_from_name(name)
  File "/opt/python/3.5.0/lib/python3.5/unittest/loader.py", line 369, in _get_module_from_name
    __import__(name)
  File "/home/travis/build/MyGitHubName/mymodule/mymodule/__init__.py", line 1, in <module>
    from mymodule import MyModule
ImportError: cannot import name 'MyModule'

My test script has the following imports:

import unittest
from mock import patch
from mymodule import MyModule

On Travis, I have attempted to run the tests via both

python setup.py test

and

python -m unittest discover

My __init__.py for mymodule looks like this:

from mymodule import MyModule

mymodule.py contains the MyModule class.

Again, these work on Python 2.7 but not 3.4 or 3.5 due to the import errors. What do I need to adjust on my imports to allow my tests to run in Python 2.7 and Python 3.4/3.5?

1 Answers1

0

Assuming you have project structure like this:

enter image description here

You will indeed get that error, because you should be more explicit about your import in python3: Changes in import statement python3

So, just edit import statement in mymodule/__init__.py to be explicit:

from .mymodule import MyModule

Community
  • 1
  • 1
Alexey
  • 1,366
  • 1
  • 13
  • 33