[英]python importing module ImportError
I am writing custom test runner for django app. 我正在为Django应用编写自定义测试运行程序。 In django app folder i got folder like this one
在Django应用文件夹中,我得到了像这样的文件夹
But when i try to run tests I got error: 但是当我尝试运行测试时出现错误:
ERROR: AdminLoginTest (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: AdminLoginTest
Traceback (most recent call last):
File "D:\\python27\lib\unittest\loader.py", line 252, in _find_tests
module = self._get_module_from_name(name)
File "D:\\python27\lib\unittest\loader.py", line 230, in _get_module_f
rom_name
__import__(name)
File "D:\\selenium_tests\tests\admin_panel\AdminLoginTest.py",
line 1, in <module>
from selenium_tests.SeleniumTestCase import SeleniumTestCase
ImportError: No module named selenium_tests.SeleniumTestCase
In SeleniumTestCase i got sth like this: 在SeleniumTestCase中,我得到了这样的东西:
class SeleniumTestCase(TestCase):
body
I believe it is something wrong with package not with my runner. 我相信套件而不是我的跑步者有问题。 Thanks for any help in advance.
感谢您的任何帮助。
It does not work because, if I am right you run as a script (by running the unittests) the file AdminLoginTest.py
. 它不起作用是因为,如果我对,那么您将作为脚本运行(通过运行unittests)文件
AdminLoginTest.py
。 Running this script, the python interpreter cannot reach the directory of selenium_tests
. 运行此脚本,python解释器无法到达
selenium_tests
的目录。
One solution would be to use relative imports, ie 一种解决方案是使用相对进口,即
from ..selenium_tests.SeleniumTestCase import SeleniumTestCase
However this will not either work because you are running the file AdminLoginTest.py
as a script. 但是,这都不起作用,因为您正在以脚本形式运行文件
AdminLoginTest.py
。 Only if the file AdminLoginTest.py
would be imported as a module you would be able to use relative imports. 只有将文件
AdminLoginTest.py
作为模块导入,您才可以使用相对导入。
I would propose to change the structure of your directory, having the unittests as a top level directory and all the other classes as lower level directories. 我建议更改目录的结构,将单元测试作为顶层目录,将所有其他类作为底层目录。 The import would then work.
导入将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.