Python ConfigParser cannot search .ini file correctly (Ubuntu 14, Python 3.4)

Problem:
The code compiles fine but when ever i call the read_db_config function i get “Exception: mysql not found in the mysql_config.ini file”

The file is in the same directory but the main script runs two directories up using

import sys 
from Config.MySQL.python_mysql_dbconfig import read_db_config

I am new to python and have searched everywhere but i cannot seem to pinpoint my issue

Code:

from ConfigParser import ConfigParser

def read_db_config(filename='mysql_config.ini', section='mysql'):

    # create parser and read ini configuration file
    parser = ConfigParser()
    parser.read(filename)

    # get section, default to mysql
    db = {}
    if parser.has_section(section):
        items = parser.items(section)
        for item in items:
            db[item[0]] = item[1]
    else:
        raise Exception('{0} not found in the {1}  file'.format(section, filename))

    return db

mysql_config.ini:

[mysql]
database = testdba
user = root
password = test
unix_socket = /opt/lampp/var/mysql/mysql.sock

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

if you use relative paths for file or directory names python will look for them (or create them) in your current working directory (the $PWD variable in bash).

if you want to have them relative to the current python file, you can use (python 3.4)

from pathlib import Path
HERE = Path(__file__).parent.resolve()
CONFIG_PATH = HERE / '../etc/mysql_config.ini'

or (python 2.7)

import os.path
HERE = os.path.abspath(os.path.dirname(__file__))
CONFIG_PATH = os.path.join(HERE, '../etc/mysql_config.ini')

if your mysql_config.ini file lives in the etc directory below your python script.

you could of course always use absolute paths (starting with a /; i.e. /home/someuser/mysql_config.ini).

Method 2

I ran it again but with the modification of adding

parser = configparser.ConfigParser()
parser['mysql'] = {'database': 'testdba',
                   'user' : 'root',
                   'password' : 'test',
                   'unix_socket' : '/opt/lampp/var/mysql/mysql.sock'}
with open('mysql_config.ini', 'w') as configfile:
parser.write(configfile

and I found that this created the file “mysql_config.ini” not in the directory where the python “read_db_config” was stored but the parent directory of main python module which calls this module. I searched it up a bit and figured out a perment solution the lets me keep the “mysql_config.ini” where I wish.

import configparser 

def read_db_config(dirname = '/opt/Python_MySQL_Email_Receipt_Client/Config/MySQL/', filename='mysql_config.ini', section='mysql'):

# create parser and read ini configuration file

parser = configparser.ConfigParser()
parser.read(dirname + filename)

# get section, default to mysql
db = {}
if parser.has_section(section):
    items = parser.items(section)
    for item in items:
        db[item[0]] = item[1]
else:
    raise Exception('{0} not found in the {1} file'.format(section, filename))

return db


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x