简体   繁体   中英

python importing module ImportError

I am writing custom test runner for django app. In django app folder i got folder like this one 我的包裹的文件夹结构

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:

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 . Running this script, the python interpreter cannot reach the directory of 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. Only if the file AdminLoginTest.py would be imported as a module you would be able to use relative imports.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM