ModuleNotFoundError: No module named 'src' (Python)



我知道这个问题之前已经问过很多次了,尽管,即使在使用绝对路径之后,我也无法越过这个导入错误

我想从functions.py中导入extensions

functions.py

from src.Categorize_CLI.extensions import *

误差

(.venv) PS D:PythonCategorize-CLI> & d:/Python/Categorize-CLI/.venv/Scripts/python.exe d:/Python/Categorize-CLI/src/Categorize_CLI/services/key_functions.py
Traceback (most recent call last):
File "d:PythonCategorize-CLIsrcCategorize_CLIserviceskey_functions.py", line 4, in <module>
from src.Categorize_CLI.extensions import *
ModuleNotFoundError: No module named 'src'

我删除了src文件夹,使Categorize_CLI成为顶级模块,但我仍然得到相同的错误:

Traceback (most recent call last):
File "d:PythonCategorize-CLICategorize_CLImain.py", line 4, in <module>
from services.functions import *
File "d:PythonCategorize-CLICategorize_CLIservicesfunctions.py", line 6, in <module>
from secondary_functions import *
ModuleNotFoundError: No module named 'secondary_functions'

这个错误是由于运行main.py

import statement in main.py

from services.functions import *

当前文件结构

Categorize-CLI
├── Categorize_CLI
│   ├── main.py
│   ├── __init__.py
│   ├── services
│   │   ├── extensions.py
│   │   ├── functions.py
│   │   ├── secondary_functions.py
│   │   └── __init__.py
├── README.md
└── .gitignore

如何从secondary_functions导入extensions:

from extensions import *

我试着克隆你的项目,试图了解发生了什么。从你问题中的图片,我没有看清楚你的文件结构。

  1. 您不需要将src声明为模块。您的顶级模块应该是Categorize_CLI(src/Categorize_CLI)。你应该删除src/__init__.py.

  2. 由于secondary_functions.pyextensions.py都已经在顶级模块中,你不需要命名它来引用它,你可以简单地在secondary_functions中导入extensions

# secondary_functions.py
from extensions import *

如果在main.py

中使用from secondary_functions import *,则此操作有效。

最新更新