ModulenotFoundError:No模块名为' src' (Python)

发布于 2025-01-23 09:44:21 字数 1682 浏览 3 评论 0原文

我知道这个问题已经多次问过,尽管即使使用了绝对路径,我也无法通过此导入错误,

我想从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月牙弯弯 2025-01-30 09:44:21

我试图克隆您的项目,以尝试了解“ S发生了什么”。我从问题中的图像中不明确地了解您的文件结构。

  1. 您不需要声明src模块。

  2. 因为secondary_functions.pyextensions.py均已在顶级模块中,您无需命名它即可引用它,您可以简单地导入扩展 secondary_functions with

# secondary_functions.py
from extensions import *

此项工作,如果您从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.

  1. You don't need to declare src as a module. Your top level module should be Categorize_CLI (src/Categorize_CLI). You should remove the src/__init__.py.

  2. Since secondary_functions.py and extensions.py are both already in top level module, you don't need to name it to refer to it, you can simply import extensions in secondary_functions with

# secondary_functions.py
from extensions import *

This works if you use from secondary_functions import * in your main.py

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文