Intellij Idea ModuleNotFoundError:使用pyb -v构建python项目时没有名为'src'的模块

thigvfpy  于 2023-04-19  发布在  Python
关注(0)|答案(1)|浏览(136)

你好,我正在做一个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
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配置为我可以点击按钮运行:

ifmq2ha2

ifmq2ha21#

我知道这个问题是一年多前发布的,但我发现自己处于同样的情况:我使用 pybuilder 创建了一个新项目,创建了一个或两个初始类和相关的单元测试,成功地运行了 pyb clean run_unit_tests,然后将其导入IntelliJ Ultimate。
对于新导入的项目,IDE中的intellisense将建议将模块导入修改为 src/path/to/your/module 的形式。如果您接受IDE更改,则您将在IntelliJ中拥有可测试的运行配置,但 pybuilder 将为您提供上面在原始问题中显示的错误类型。
解决方案是通过控制台运行 pyb clean install,以确保您的模块安装到 venv 中。安装完成后,IDE应在单元测试类的导入中获取模块引用。
为了完整起见,我只是使用 pybuilder 快速创建了一个示例项目,并在IntelliJ中打开之前测试了以下内容:

| --src
     | -- _init__.py
     | -- main 
            | -- __init__.py
            | -- python
                   | -- __init.py__
                   | -- hello_world.py
 
     | -- unittest
            | -- __init__.py
            | -- python
                   | -- __init__.py
                   | -- hello_world_tests.py

使用 hello_world.py,如下所示:

class hello_world:

    def __init__(self, addressee):
        self.addressee = addressee

    def greeting(self):
        return 'Hello ' + self.addressee

...和hello_world_tests.py:

import unittest

from hello_world import hello_world

class MyTestCase(unittest.TestCase):
    def test_something(self):

        my_object = hello_world('Bob')

        self.assertEqual(my_object.greeting(), "Hello Bob")

if __name__ == '__main__':
    unittest.main()

然后,在通过控制台运行pyb clean install之后,测试类就可以在IDE中运行了。希望这对将来的其他人有所帮助。

相关问题