简体   繁体   English

PyCharm Django 测试 ModuleNotFoundError

[英]PyCharm Django test ModuleNotFoundError

Project tree:项目树:

|___sb_website
|   |___article
|   |   |__...py
|   |___home
|   |   |__...py
|   |___sb_website
|   |   |__dev_settings.py
|   |   |__...py
|   |__manage.py
|   |__...py

My django project development settings are dev_settings.py .我的 django 项目开发设置是dev_settings.py I want to run my tests against these settings.我想针对这些设置运行我的测试。

When I use a Python configuration in PyCharm with these parameters test --settings=sb_website.dev_settings , script path ..\sb_website\manage.py I get the following Error: ERROR: sb_website.article (unittest.loader._FailedTest) ImportError: Failed to import test module: sb_website.article .当我在PyCharm中使用带有这些参数测试的Python 配置test --settings=sb_website.dev_settings ,脚本路径..\sb_website\manage.py我收到以下错误: ERROR: sb_website.article (unittest.loader._FailedTest) ImportError: Failed to import test module: sb_website.article Same for sb_website.home and sb_website.sb_website sb_website.homesb_website.sb_website相同

If use a Django Test configuration in PyCharm modules are imported and tests run fine.如果使用Django 中的测试配置PyCharm模块被导入并且测试运行正常。

I'd like to learn the root cause of my problem as it is not the first time I faced this.我想了解问题的根本原因,因为这不是我第一次遇到这个问题。

What I tried: changed all imports to explicit imports, eg *.models to home.models我尝试了什么:将所有导入更改为显式导入,例如*.modelshome.models

The issue was not related to PyCharm, the answer why there was a difference between the Python configuration and Django Test configuration was that the Python configuration pointed directly to the test target.该问题与 PyCharm 无关, Python 配置Django 测试配置之间存在差异的答案是Python 配置直接指向测试目标。 Django Test configuration uses the manage.py test command and executing it tried to import tests and found a __init__.py in the root folder. Django 测试配置使用manage.py test命令并执行它尝试导入测试并在根文件夹中找到__init__.py

By removing the __ini__.py in my root folder of sb_website the modules were found correctly and tests ran through.通过删除我的__ini__.py根文件夹中的sb_website ,可以正确找到模块并运行测试。 The answer was found here Running django tests fails答案在这里找到运行 django 测试失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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