[英]Import error running unittest in Python3
I have a problem importing files in Python 3.6.我在 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
:它使用
b.py
中的以下导入语句运行我的应用程序(但是,没有运行测试):
from a import *
But, it does not work my application (but, works the tests) using this other in b.py
:但是,它不适用于我的应用程序(但是,可以在测试中使用)在
b.py
中使用另一个:
from .a import *
So, I choose from a import *
.所以,我
from a import *
中选择。 Executing test like python3 -m unittest
I always get following error:执行像
python3 -m unittest
这样的测试我总是得到以下错误:
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:在这种情况下,我在
test_b.py
中的导入语句如下所示:
from unittest import TestCase
from app.cell import *
Is there any way to fix this problem?有没有办法解决这个问题?
I was confused with imports in Python and how python works with modules. 我对Python中的导入以及python如何使用模块感到困惑。
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
: 在
main.py
:
from module.b import Something
In b.py
: 在
b.py
:
from .a import Something
In a.py
: 在
a.py
:
from unittest import TestCase
from module.a import Something
In test_b.py
: 在
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. 像这样,它工作正常,应用程序和测试。
IMHO for a user friendly and intuitive language as Python, it's import system is one of most unfriendly I've seen around.恕我直言,Python 是一种用户友好且直观的语言,它的导入系统是我见过的最不友好的系统之一。
Ugly as it might be I usually do something like (python 3.6+):尽管可能很丑,但我通常会做类似(python 3.6+)的事情:
try:
from a import class_defined_in_a_py
except (ModuleNotFoundError, ImportError):
from .a import class_defined_in_a_py
First import works for app, second works for tests.第一个导入适用于应用程序,第二个适用于测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.