ModulenotFoundError:No模块名为' src' (Python)
我知道这个问题已经多次问过,尽管即使使用了绝对路径,我也无法通过此导入错误,
我想从function.py
< extensions
strong> functions.py
from src.Categorize_CLI.extensions import *
错误
(.venv) PS D:\Python\Categorize-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:\Python\Categorize-CLI\src\Categorize_CLI\services\key_functions.py", line 4, in <module>
from src.Categorize_CLI.extensions import *
ModuleNotFoundError: No module named 'src'
更新
我删除了src
文件夹制作epcorize_cli_cli
顶级模块,但我仍然遇到相同的错误:
Traceback (most recent call last):
File "d:\Python\Categorize-CLI\Categorize_CLI\main.py", line 4, in <module>
from services.functions import *
File "d:\Python\Categorize-CLI\Categorize_CLI\services\functions.py", line 6, in <module>
from secondary_functions import *
ModuleNotFoundError: No module named 'secondary_functions'
此错误来自运行main.py
inmain..py.py.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
:
from extensions import *
I know this question has been asked multiple times before although, even after using absolute paths I can't get past this import error
I want to import extensions
from functions.py
functions.py
from src.Categorize_CLI.extensions import *
Error
(.venv) PS D:\Python\Categorize-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:\Python\Categorize-CLI\src\Categorize_CLI\services\key_functions.py", line 4, in <module>
from src.Categorize_CLI.extensions import *
ModuleNotFoundError: No module named 'src'
Update
I removed the src
folder making Categorize_CLI
the top level module, but I still get the same error:
Traceback (most recent call last):
File "d:\Python\Categorize-CLI\Categorize_CLI\main.py", line 4, in <module>
from services.functions import *
File "d:\Python\Categorize-CLI\Categorize_CLI\services\functions.py", line 6, in <module>
from secondary_functions import *
ModuleNotFoundError: No module named 'secondary_functions'
This error is from running main.py
import statement in main.py
from services.functions import *
Current file structure
Categorize-CLI
├── Categorize_CLI
│ ├── main.py
│ ├── __init__.py
│ ├── services
│ │ ├── extensions.py
│ │ ├── functions.py
│ │ ├── secondary_functions.py
│ │ └── __init__.py
├── README.md
└── .gitignore
How I am importing extensions
from secondary_functions
:
from extensions import *
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我试图克隆您的项目,以尝试了解“ S发生了什么”。我从问题中的图像中不明确地了解您的文件结构。
您不需要声明
src
模块。因为
secondary_functions.py
和extensions.py
均已在顶级模块中,您无需命名它即可引用它,您可以简单地导入扩展
secondary_functions
with此项工作,如果您从secondary_functions中使用
import *
在main.main.py.py
中I tried to clone your project to try understand what"s happening. I didn't understand clearly your file structure from the image in your question.
You don't need to declare
src
as a module. Your top level module should beCategorize_CLI
(src/Categorize_CLI
). You should remove thesrc/__init__.py
.Since
secondary_functions.py
andextensions.py
are both already in top level module, you don't need to name it to refer to it, you can simply importextensions
insecondary_functions
withThis works if you use
from secondary_functions import *
in yourmain.py