【问题标题】:ModuleNotFoundError: No module named 'src' in pythonModuleNotFoundError:python 中没有名为“src”的模块
【发布时间】:2020-12-19 20:29:11
【问题描述】:

我有以下文件夹结构:

src
 |_ __init__.py
    example.py
test
 |_ test.py
# __init__.py
class API:
    def something(self):
        print('folder src | file __init__')
# example.py
class Example:
    def doingSomething(self):
        print('folder src | file example')
# test.py
import src
from src.example import Example

class Test:
    def somethingElse(self):
        print('folder test | file test')

当我运行test.py 文件时,我收到以下错误:

Traceback (most recent call last):
  File "<my path>\test\test.py", line 1, in <module>
    import src
ModuleNotFoundError: No module named 'src'

【问题讨论】:

    标签: python


    【解决方案1】:

    除非您在src/__init__.py 文件中导入example 模块,否则您需要在包(即src)中指定模块名称(即example)。

    from src.example import Example
    

    【讨论】:

    • 好的,谢谢,但我仍然从 test.py 的第一行得到同样的错误
    • 表示src包不在你的PYTHONPATH
    • 如何将它放入我的 PYTHONPATH 中?我正在使用 vscode
    • This questionthis one 可能有用。
    • 尝试使用下面的 import sys sys.path.append('../')
    猜你喜欢
    • 2021-11-06
    • 2019-07-30
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 2022-07-21
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多