首页 > 解决方案 > Persistent ModuleNotFoundError: No module named after setting path to project on Windows OS

问题描述

在我尝试coverage run mytestsuite.py -v在此代码的终端中运行覆盖范围时:

import unittest
import coverage
import mytestsuite as testSuite

if __name__ == '__main__':
    cov = coverage.Coverage()
    cov.start()
    framework = unittest.TestLoader().loadTestsFromModule(testSuite)

    test_result = unittest.TextTestRunner(verbosity=2).run(framework).wasSuccessful()
    cov.stop()
    cov.save()

    print("\nTesting Concluded with result:", test_result)
    cov.html_report()

我观察到以下错误: ModuleNotFoundError: No module named 'mysource.py'

其中 mysource.py 使用以下语法导入到 mytestsuite.py 中: import sourcefolder.mysource as src

如果我在 IDE 中运行 mytestsuite.py,我会看到以下输出:

-------------------------------------------------- -------------------- 在 0.000 秒内运行 0 次测试

OK Coverage.py 警告:未收集数据。(no-data-collected) Traceback(最近一次通话最后一次):文件“C:\Users\Anthem Rukiya\IdeaProjects\DiTTo_YoutubePredictor\test\youtubePredictor_testRunner.py”,第 33 行,在 cov.html_report() 文件“C:\用户\Anthem Rukiya\AppData\Local\Programs\Python\Python39\lib\site-packages\coverage\control.py",第 972 行,在 html_report 返回reporter.report(morfs) 文件“C:\Users\Anthem Rukiya\ AppData\Local\Programs\Python\Python39\lib\site-packages\coverage\html.py”,第 241 行,在报告中为 fr,get_analysis_to_report(self.coverage,morfs)中的分析:文件“C:\Users\Anthem Rukiya\AppData\Local\Programs\Python\Python39\lib\site-packages\coverage\report.py",第 66 行,在 get_analysis_to_report 中引发 CoverageException("No data to report.") coverage.misc。

测试结果:真

进程以退出代码 1 结束

我目前在 IntelliJ 的 Windows 操作系统上运行 Python 3.9.2,并尝试在命令提示符下使用以下命令将我的项目文件设置为 python 路径:set PYTHONPATH%PYTHONPATH%;C:\path_to_my_project_folder

有什么建议么?

标签: python-3.xwindowscode-coveragepython-unittest

解决方案


推荐阅读