Forums

No module named 'config' when importing from the same directory in Python project

I'm working on a Python project with the following directory structure:

lunarcity/
├── main.py
└── modules/
    ├── __init__.py
    ├── config.py
    ├── voice.py
    ├── manager.py
    └── other_module.py

In the main.py file, I have a handle_voice_message function that is supposed to import and use functions from the voice.py file located in the modules directory. Additionally, voice.py imports functions from config.py and manager.py, both of which are also in the same modules directory.

However, when I run the main.py file, I encounter the following error: ModuleNotFoundError: No module named 'config'

I have tried various import methods, including both direct imports like import config and relative imports like from . import config, but none of them seem to resolve the issue. It's worth mentioning that I have added the __init__.py file to the modules directory to make it a package.

I have already tried: 1. Checking for typos in the file names and import statements. 2. Ensuring that the init.py file is present in the modules directory to make it a package. 3. Trying both direct and relative import statements in voice.py.

Despite all these efforts, I still can't seem to import the modules from the same directory. I'm puzzled by this behavior, and I don't understand why the imports are not working as expected...

Take a look at our help page