Should I use ConfigParser
which is compatible with python 2.7 and 3.x or do you suggest any other module in python which is compatible with both versions of python for reading config file?

- 49
- 2
- 6
-
Apart from the name of the module, what exactly do you think is incompatible between the versions? – Daniel Roseman Dec 10 '14 at 10:52
-
"from ConfigParser import SafeConfigParser" this line giving error in python3 which will work great in python 2.7 – NIlesh Meharkar Dec 10 '14 at 10:55
-
i want to create an app which will work on both the versions of python – NIlesh Meharkar Dec 10 '14 at 10:56
3 Answers
You can make use of configparser
backport, so it will work on both Python version.
pip install configparser

- 1,191
- 8
- 9
Contrary to the other answers here, you don't need to install any extra packages to write INI-parsing code that is compatible with Python 2 and 3. Python 3.0's configparser
is just a renamed version of Python 2's ConfigParser
. There have been some extra features added in Python 3.2 and 3.5 (see the latest version of the docs at https://docs.python.org/library/configparser.html) but these are backwards-compatible, so if you're happy with the features of Python 3.0's configparser
you can just do
try:
import configparser
except ImportError:
# Python 2.x fallback
import ConfigParser as configparser

- 143,130
- 81
- 406
- 459
-
This would be more helpful if it made more explicit just what set of features of python3's configparser might be used to maintain backward compatibility with python2's ConfigParser. For example, python3 does not have a SafeConfigParser class (its equivalent is now ConfigParser). – Jacob Lee Aug 10 '18 at 19:35
You can use the configparser2
module, which is a fork of the configparser
backport maintained by me.
Add it in your requirements or do pip install configparser2
and then use something as simple as:
try:
import configparser
except ImportError as e:
import configparser2 as configparser

- 143,130
- 81
- 406
- 459

- 47,722
- 9
- 78
- 80