I'm currently trying to import a module on Python but I'm receiving the following message:
Traceback (most recent call last):
File "test_db_conn.py", line 8, in <module>
from config import config_reader
ModuleNotFoundError: No module named 'config'
Here's the folder structure:
config
- __init.py
- config_reader.py
- config.ini
db_functions
- test_db_conn.py
And the code on 'test_db_conn.py':
# Import modules
from config import config_reader
import pymongo
# Query database and print result
db_client = pymongo.MongoClient(db_url)
db_status = db_client.test
print(db_status)
Are you able to help me with this?
Thank you in advance
davidism
128k31 gold badges417 silver badges348 bronze badges
1 Answer 1
You need to append the parent path of your project to the Python module search directory. I replicated your folder structure locally on my dev system, and was able to access a function named config_reader in the config_read.py module, within the config package.
import sys
import pathlib
parent_dir = pathlib.Path(__file__).parent.parent
sys.path.append(parent_dir.__str__())
from config.config_reader import config_reader
config_reader()
answered Aug 7, 2021 at 0:22
user189198
Sign up to request clarification or add additional context in comments.
lang-py
import config_reader