7

I have a problem importing files in Python 3.6. My directories tree is as given below:

project/
    app/
    ├── __init__.py
    ├── a.py
    └── b.py
    test/
    ├── __init__.py
    ├── test_a.py
    └── test_b.py

It works my application (but, no works the tests) using following import statement in b.py:

from a import *

But, it does not work my application (but, works the tests) using this other in b.py:

from .a import *

So, I choose from a import *. Executing test like python3 -m unittest I always get following error:

E.
======================================================================
ERROR: tests.test_cell (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.test_cell
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 428, in _find_test_path
    module = self._get_module_from_name(name)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name
    __import__(name)
  File "/Users/serrodcal/Repositories/project/tests/test_b.py", line 2, in <module>
    from app.b import *
  File "/Users/serrodcal/Repositories/project/app/b.py", line 1, in <module>
    from a import *
ModuleNotFoundError: No module named 'a'


----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (errors=1)

In this case, my import statement in test_b.py is as given below:

from unittest import TestCase
from app.cell import *

Is there any way to fix this problem?

6
  • Can you post the content of you __init__.py? Commented Jun 2, 2017 at 6:40
  • I think it's easier for you to share the repository. There you can see all the information because in this post I do not pretend to show my specific case. Commented Jun 2, 2017 at 6:45
  • Regarding your question, __init__.py is empty, like many example that I could see. Commented Jun 2, 2017 at 6:46
  • psst, please check out pytest, it resembles idiomatic Python rather than re-using Java patterns and there's a fat chance you'll enjoy it much more Commented Jun 2, 2017 at 9:04
  • Yep, that is more Pythonic but unittest is included and I do not want to import more libraries. Commented Jun 2, 2017 at 10:38

1 Answer 1

4

I was confused with imports in Python and how python works with modules.

project/
    module/
        __init__.py
        a.py
        b.py
    test/
        test_a.py
        test_b.py
    main.py

This is my new directories tree. The contains of the files are:

In main.py:

from module.b import Something

In b.py:

from .a import Something

In test_a.py:

from unittest import TestCase
from module.a import Something

In test_b.py:

from unittest import TestCase
from module.a import Something
from module.b import Something

Like this, it works fine, application and tests.

Sign up to request clarification or add additional context in comments.

3 Comments

Why should the structure be like this?
I think you wanted to say: 'In test_a.py' instead of 'In a.py'.
It is also important to mention that 'module' must be installed in site-packages. Otherwise, you will have to add it to sys.path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.