0

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

2
  • your test file is already inside of config folder so you only need import config_reader Commented Aug 7, 2021 at 0:01
  • Sorry, that "folder structure" is wrong. I mistyped while creating the post. The test script is inside "db_functions" that is on the same level as the "config" folder. Commented Aug 7, 2021 at 0:03

1 Answer 1

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

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply! I'll test it as soon as I get to my computer. I'll update you than.
Thank you for the tip! It didn't work with that exact same code but you point me in the right direction to fix the issue! Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.