rmso27
rmso27

Reputation: 9

Import module on Python

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

Upvotes: 0

Views: 190

Answers (1)

user189198
user189198

Reputation:

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()

enter image description here

Upvotes: 1

Related Questions