ModuleNotFoundError:使用pyb v构建python项目时,没有名为“src”的模块

2024-10-06 12:31:34 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我正在处理一个python项目,并使用pybuilder创建python项目。结构如下:


    | --src
         | -- _init__.py
         | -- main 
                | -- __init__.py
                | -- python
                       | -- __init.py__
                       | -- amass
                              | -- amass.py
     
         | -- unittest
                | -- __init__.py
                | -- python
                       | -- __init__.py
                       | -- my_tests.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\u tests.MyTestCase.test\u子域\u finding\u 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配置为我可以单击按钮运行吗 : enter image description here


Tags: toinpytestimportinfosrcproject