Skip to content Skip to sidebar Skip to footer

Configparser Not Working In Python 3.4, Nosectionerror But Works Fine In Pycharm

I worked on a Python 3.4 script in PyCharm 4.5. (repo: https://github.com/Djidiouf/bbot ) In it, I used import configparser without any problem for retrieving some values in a conf

Solution 1:

The install error is from _KEYCRE = re.compile(ur"%\(([^)]+)\)s"), that is causing a syntax error as the ur prefix is not supported in python3, pip is trying to instal a python2 package.

Your first error is a keyError 'bot_configuration' which is not because of not having configparser installed, configparser is part of the standard library.

The bot_configurationKeyError is because python cannot find your config file, if you use config.read('foobar') in pycharm you will see the exact same output

Solution 2:

I figured out that I had some inconsistency about running the script. When I was in another folder than my script, nothing worked but when I was in it, it worked. Perhaps because the config file is shared and used in the main script and in a module. In fact it's due to the need to have an absolute path in config.read('/some/path/file.cfg')

Finally, I find that answer: https://stackoverflow.com/a/13800583/3301370


So here is the solution in the case of config.cfg being on the project root directory /config.cfg :

/bbot.py

import osconfig.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'config.cfg'))

However, as the config file is shared and used in the main script and in a subpath, module, a correct path must be set for the sub module too:

/modules/connection.py

import osconfig.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'config.cfg'))

It works fine in PyCharm because when you hit the run button for your script, it runs it with the path of the project. The fact that having a relative path for both config.read() statements worked only because of that.

The problem with installing configparser with pip is unrelated to that specific problem and can be ignore in that case as configparser is a built-in library in Python3.

Post a Comment for "Configparser Not Working In Python 3.4, Nosectionerror But Works Fine In Pycharm"