简体   繁体   English

python导入模块ImportError

[英]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.

相关问题 将python包导入python模块时ImportError - ImportError while importing a python package into a python module 导入python的winreg模块时ImportError - ImportError while importing winreg module of python 导入Python包-“ ImportError:未命名模块...” - Importing Python package - “ImportError: No module named…” “ ImportError:没有名为xhaus的模块” Python模块从ansible导入错误 - “ImportError: No module named xhaus” Python module importing error from ansible mod_python和子包导入问题:ImportError:未命名模块 - mod_python and subpackages importing issues: ImportError: No module named Python-导入paypalrestsdk导致ImportError:没有名为_winreg的模块 - Python - Importing paypalrestsdk causes ImportError: No module named _winreg 跨包导入模块时,Python“ ImportError:未命名模块” - Python 'ImportError: No module named' when importing modules across packages 导入python azure模块storage.table时发生ImportError - ImportError while importing python azure module storage.table 在Eclipse中将Python模块导入Java程序时的ImportError - ImportError when Importing Python Module into Java Program in Eclipse 导入Pocketsphinx无法在python 3.2(Windows)中运行。 “ ImportError:没有名为pocketsphinx的模块” - Importing pocketsphinx not working python 3.2 (Windows). “ImportError: No module named pocketsphinx”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM