I am getting this error --
Traceback (most recent call last):
File "F:\Coding\user.py", line 2, in <module>
from db_handler.handler import check
File "F:\Coding\db_handler\handler.py", line 14, in <module>
params = db_config()
^^^^^^^^^^^
File "F:\Coding\db_handler\config.py", line 22, in db_config
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
Exception: Section db_info not found in the config.ini file
This is my file structure :
├db_handler
| └───config.ini
| └───config.py
| └───handler.py
| └───__init__.py
├user.py
This is my config.ini file
[db_info]
host=somesite.com
database=profiles
user=hilfing
password=pass
port=5432
I am using the following code to read my config in config.py
def db_config(filename='config.ini', section='db_info'):
# create a parser
parser = ConfigParser()
# read config file
parser.read(filename)
db = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
return db
This is my handler.py
import psycopg2
from .config import db_config
# read connection parameters
params = db_config()
# connect to the PostgreSQL server
try:
conn = psycopg2.connect(**params)
except (Exception, psycopg2.DatabaseError) as error:
print(error)
This is my user.py
from db_handler.handler import check
c = check()
print(c)
#Other code...
I searched on Google and Stackoverflow and tried adding . or ./ before opening config file in config.py but none worked.
However I checked and it seems when I remove the relative import .config -> config in handler.py, it works when I just import handler.py and not the whole package. It only breaks when importing the package as a whole.
EDIT : I figured it out with a little help. config.ini needs to be placed in root dir