python报错ModuleNotFoundError: No module named ‘configs‘

问题:项目根目录执行pytest命令报错,找不到该模块

E:\桌面\code\combat>pytest -s
Test session starts (platform: win32, Python 3.8.4, pytest 6.2.5, pytest-sugar 0.9.4)
rootdir: E:\桌面\code\combat
plugins: allure-pytest-2.9.45, html-3.1.1, metadata-1.11.0, sugar-0.9.4
collecting ... 
―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ERROR collecting test session
――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
d:\py3.8\lib\importlib\__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1014: in _gcd_import
    ???
<frozen importlib._bootstrap>:991: in _find_and_load
    ???
<frozen importlib._bootstrap>:975: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:671: in _load_unlocked
    ???
d:\py3.8\lib\site-packages\_pytest\assertion\rewrite.py:170: in exec_module
    exec(co, module.__dict__)
testCase\apiCase\business\conftest.py:5: in <module>
    from configs.env import NAME_PSW
E   ModuleNotFoundError: No module named 'configs'

=============================================================================== short test summary info ===============================================================================
FAILED  - ModuleNotFoundError: No module named 'configs'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Results (0.62s):

#报错信息
E   ModuleNotFoundError: No module named 'configs'

问题原因:

  • python寻找包和模块首先根据文件自身为起点指定相对路径,其次根据设定的pythonpath为起点指定相对路径。
  • 而现在的项目由于层级结构复杂,并不统一,因此根据文件自身为起点指定的相对路径不可用

解决办法

设定pythonpath,统一相对路径起点

1.通过python -m pytest -s 运行测试用例,python会把当前目录添加到pythonpath

E:\桌面\code\combat>python -m pytest -s
Test session starts (platform: win32, Python 3.8.4, pytest 6.2.5, pytest-sugar 0.9.4)
rootdir: E:\桌面\code\combat
plugins: allure-pytest-2.9.45, html-3.1.1, metadata-1.11.0, sugar-0.9.4
collecting ... --登录操作初始化--
--开始清楚部门数据--
tc000001部门初始化

tc000001
 testCase/apiCase/business/test_organiz.py ✓                                                                                                                              25% ██▌
--tc000091部门初始化--
--tc000091进行清楚操作--
 testCase/apiCase/business/test_organiz.py ✓✓                                                                                                                             50% █████
--新增部门的初始化--
--tc000002进行部门初始化操作--

--tc000002进行部门清除操作--
 testCase/apiCase/business/D-研发部/test_exist_organiz.py ✓                                                                                                                  75% ███████
▌  --tc000051进行部门初始化操作--
--tc000051进行部门清除操作--
--新增部门的清除--
-清楚部门数据完成--
--登录初始化完成--
 testCase/apiCase/business/D-研发部/test_exist_organiz.py ✓✓                                                                                                                100% ███████
███

Results (3.15s):
       4 passed

2.通过项目跟目录创建一个空的conftest.py文件,用pytest可以直接指定当前根目录到pythonpath
在这里插入图片描述

E:\桌面\code\combat>pytest
Test session starts (platform: win32, Python 3.8.4, pytest 6.2.5, pytest-sugar 0.9.4)
rootdir: E:\桌面\code\combat
plugins: allure-pytest-2.9.45, html-3.1.1, metadata-1.11.0, sugar-0.9.4
collecting ... 
 testCase/apiCase/business/test_organiz.py ✓                                                                                                                              25% ██▌
 testCase/apiCase/business/test_organiz.py ✓✓                                                                                                                             50% █████

 testCase/apiCase/business/D-研发部/test_exist_organiz.py ✓                                                                                                                  75% ███████
 testCase/apiCase/business/D-研发部/test_exist_organiz.py ✓✓                                                                                                                100% ███████
███

Results (2.37s):
       4 passed


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用和引用[2]提到了导致出现"ModuleNotFoundError: No module named 'config'"错误的常见情况。这个错误通常表示当前的运行环境中找不到名为'config'的Python模块,导致相应的程序无法正常执行。而你提到的"ModuleNotFoundError: No module named 'configs.model_config'"错误则表示在当前的运行环境中找不到名为'configs.model_config'的Python模块。 综合以上情况,出现这个错误的原因可能是你没有安装或配置正确的Python模块。你可以按照以下步骤解决这个问题: 1. 首先,确认你是否正确安装了名为'config'或'configs'的Python模块。你可以使用pip命令来安装缺失的模块。在命令行界面中运行以下命令: ``` pip install config ``` 或 ``` pip install configs ``` 2. 如果安装过程出现了错误,请检查你的Python环境是否正确配置。确保你使用的Python版本与所需模块兼容,并且路径设置正确。 3. 如果以上步骤都没有解决问题,那么可能是你的代码中存在错误或引用了不存在的模块。请仔细检查你的代码,确保正确导入了需要的模块,并且模块的名称和路径是正确的。 通过按照以上步骤检查和调整Python环境,并使用pip命令安装缺失的模块,你应该能够解决"ModuleNotFoundError: No module named 'configs.model_config'"错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python编程中,出现“ModuleNotFoundError: No module namedconfig’”的错误提示信息是比较常见的。...](https://blog.csdn.net/update7/article/details/129807859)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dreamer_code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值