0

I am trying to use config parser like I normally do but for some reason I am not pulling any of the sections?

My Code:

import os, configparser

## Get directory path
dir_path = os.path.dirname(os.path.realpath(__file__))
file_path = dir_path + "\\development.ini"

## Setup config parser object
config = configparser.ConfigParser()

## Read file into config parser
with open(file_path) as file:
    config.read_file(file)

print(config.sections())

My Config File:

[MYSQL]
Host = 192.168.1.11
Port = 3306
Username = server
Password = (Removed)
Database = server

Code Output:

[]

No errors and just an empty list is returned on "config.sections()"? I am confused and I am sure it is something quite simple I am missing... Any help would be appreciated.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Oddity
  • 480
  • 2
  • 12
  • did you import the config file in your code or main file ? ```from packagename import config``` – Anmol Parida Oct 06 '20 at 16:01
  • @AnmolParida I am importing the config file directly, not the main file. I retrieve the current file's path and append the config file name onto the path string to input into open. I printed the file_path as I thought this might be the case but it is correct. And when I read the file normally through python it shows the contents. – Oddity Oct 06 '20 at 16:03
  • Hope this helps https://stackoverflow.com/questions/34980163/configparser-print-config-sections-returns – Anmol Parida Oct 06 '20 at 16:07
  • @AnmolParida Thanks for the link but sadly it does not. – Oddity Oct 06 '20 at 16:11
  • can you change it to ```development.cfg``` and try – Anmol Parida Oct 06 '20 at 16:14
  • @AnmolParida it was initially .cfg and I changed it to INI thinking that may be the problem. – Oddity Oct 06 '20 at 16:15
  • I'm glad you found a solution to your problem. However, an actual answer/solution should **not** be edited into your question. In general, you should [edit] the question to *clarify* it, but not to include an answer within it. You should create your own answer with the code/solution you used to solve your problem, and then accept it (the system may require a 48 hour delay prior to doing so). When you've solved the problem yourself, [answering your own question is encouraged](/help/self-answer). – double-beep Oct 06 '20 at 18:01
  • Are you sure you are opening the file you think you do? I ran your code with the same ini file and `['MYSQL']` was printed... – Tomerikoo Oct 06 '20 at 18:08

2 Answers2

1

This is because you only have the default section. According to the doc :

Return a list of the sections available; the default section is not included in the list. https://docs.python.org/3/library/configparser.html

Then, you don't have to open the file. The config parser will do it for you.

## Setup config parser object
config = configparser.ConfigParser()

## Read file into config parser
config.read(file)

print(config.sections())

Here's an example :

config.ini :

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no

test.py :

import configparser
config = configparser.ConfigParser()
config.read('config.ini')
print(config.sections())

Output:

['bitbucket.org', 'topsecret.server.com']
Vinetos
  • 150
  • 1
  • 7
  • I have tried with multiple sections. Initially I had [MYSQL] and the same issue occurs. I will update the post to show this instead of default. EDIT: I changed my config file back to [MYSQL] and the same issue is occurring. When I had the DEFAULT section I also had the [MYSQL] section under it but only wanted to show the [MYSQL] section in the post but miscopied. Thanks for the input. – Oddity Oct 06 '20 at 16:01
  • I was originally using read but it wasn't functioning so I changed to read file thinking that might be the issue. The file is correct as when I read the file directly I see the correct information. – Oddity Oct 06 '20 at 16:16
  • I've tried with the one in the comment and it works. Can you edit your post and post the whole config file ? – Vinetos Oct 06 '20 at 16:19
  • the post is updated with my current config file that is failing. – Oddity Oct 06 '20 at 16:24
  • That's weird. Your code is definitely working on my machine. As the config is in the same folder than the script. Just put the filename and give it a try. Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222609/discussion-between-vinetos-and-oddity). – Vinetos Oct 06 '20 at 16:29
  • I edited my question with how it was fixed. Thanks for the help. – Oddity Oct 06 '20 at 16:34
1

I guess the issue is in accessing the file only. I tried with below code with both files in one folder, runs fine. Try changing to .cfg

from configparser import ConfigParser

config = ConfigParser()
config.read("dev.cfg")

print(config.sections())

or your code

import os, configparser

## Get directory path
dir_path = os.path.dirname(os.path.realpath(__file__))
file_path = dir_path + "\\dev.cfg"

## Setup config parser object
config = configparser.ConfigParser()

## Read file into config parser
with open(file_path) as file:
    config.read_file(file)

print(config.sections())
Anmol Parida
  • 672
  • 5
  • 16
  • As I mentioned to your comment above it was originally cfg and I changed it to INI as I thought this was the problem. I changed it back to cfg to test and I get the same result. – Oddity Oct 06 '20 at 16:25