pythonpippython-importfile-structure

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

  • 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