'ModuleNotFoundError: No module named 'src' (Python)
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 *
Solution 1:[1]
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
srcas a module. Your top level module should beCategorize_CLI(src/Categorize_CLI). You should remove thesrc/__init__.py.Since
secondary_functions.pyandextensions.pyare both already in top level module, you don't need to name it to refer to it, you can simply importextensionsinsecondary_functionswith
# secondary_functions.py
from extensions import *
This works if you use from secondary_functions import * in your main.py
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peterrabbit |
