首页 > 解决方案 > ModuleNotFoundError:使用 pyb -v 构建 python 项目时没有名为“src”的模块

问题描述

嗨,我正在开发一个 python 项目,并使用 pybuilder 创建 python 项目。结构是这样的:


    | --src
         | -- _init__.py
         | -- main 
                | -- __init__.py
                | -- python
                       | -- __init.py__
                       | -- amass
                              | -- amass.py
     
         | -- unittest
                | -- __init__.py
                | -- python
                       | -- __init__.py
                       | -- my_tests.py

amass.py我有我的主要功能并且在测试my_tests.py中,我只想简单地测试:这里,my_tests.py


    import unittest
    from src.main.python.amass import amass
    
    class MyTestCase(unittest.TestCase):
    
        def test_something(self):
            self.assertEqual(True, True)
    
    
        def test_subdomains_finding_amass(self):
            self.assertTrue(amass.find_subdomains()) # main func in amass.py
    
    
    if __name__ == '__main__':
        unittest.main()

但是当我运行 pyb -v 时,它有错误:

```
[INFO]  Processing dependency packages 'edgegrid-python==1.2.0' to be installed with {}
[INFO]  Processing dependency packages 'mock==4.0.1' to be installed with {}
[INFO]  Processing dependency packages 'moto==1.3.14' to be installed with {}
[INFO]  Processing dependency packages 'pandas==1.3.3' to be installed with {}
[INFO]  Processing dependency packages 'requests==2.20.1' to be installed with {}
[INFO]  Requested coverage for tasks: pybuilder.plugins.python.unittest_plugin:run_unit_tests
[INFO]  Running unit tests
[INFO]  Executing unit tests from Python modules in /Users/hbu/Work/Project-Beacon/src/unittest/python
[INFO]  Executed 1 unit tests
[ERROR] Test has error: unittest.loader._FailedTest.rest_tests
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.7/3.7.10_3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/local/Cellar/python@3.7/3.7.10_3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 628, in run
    testMethod()
  File "/usr/local/Cellar/python@3.7/3.7.10_3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 34, in testFailure
    raise self._exception
ImportError: Failed to import test module: rest_tests
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.7/3.7.10_3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "/Users/xxx/Work/Project-Beacon/src/unittest/python/my_tests.py", line 2, in <module>
    from src.main.python.amass import amass
ModuleNotFoundError: No module named 'src'


------------------------------------------------------------
BUILD FAILED - There were 1 error(s) and 0 failure(s) in unit tests (pybuilder/plugins/python/unittest_plugin.py)
------------------------------------------------------------
```

这是我的 build.py:

```
from pybuilder.core import init, use_plugin

version = "0.0.1"

use_plugin("python.core")
use_plugin("python.install_dependencies")
use_plugin("python.unittest")
use_plugin("python.coverage")
use_plugin("python.flake8")
use_plugin('python.pycharm')
use_plugin('pypi:pybuilder_aws_plugin')

default_task = ["clean", "analyze", "publish", "package_lambda_code"]


@init
def initialize(project):
    project.name = "finddomain-" + version
    project.depends_on('pandas', version="==1.3.3")
    project.depends_on('edgegrid-python', version="==1.2.0")
    project.depends_on('mock', version="==4.0.1")
    project.depends_on('moto', version="==1.3.14")
    project.depends_on('requests', version="==2.20.1")
    project.build_depends_on("flake8")

    project.set_property('coverage_threshold_warn', 90)
    project.set_property('coverage_break_build', False)
    project.set_property('dir_source_unittest_python', 'src/unittest/python')

    project.set_property('flake8_break_build', False)
    project.set_property('flake8_max_line_length', 200)
    project.set_property('flake8_include_test_sources', True)

```

我试过了

python -m unittest rest_tests.MyTestCase.test_subdomains_finding_amass


ImportError: Failed to import test module: rest_tests

Traceback (most recent call last):
  File 
"/usr/local/opt/python@3.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
ModuleNotFoundError: No module named 'rest_tests'

Ran 1 test in 0.000s

FAILED (errors=1)

任何人都可以帮忙吗?是不是因为intellij配置为我可以点击按钮运行: 在此处输入图像描述

标签: pythonpython-3.xunit-testingintellij-ideapybuilder

解决方案


推荐阅读