Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility of configparser with Python2 and Python3 #638

Open
Pablitosi4 opened this issue Mar 19, 2024 · 2 comments
Open

Compatibility of configparser with Python2 and Python3 #638

Pablitosi4 opened this issue Mar 19, 2024 · 2 comments

Comments

@Pablitosi4
Copy link

Hello, I am working on migrating a project from Python 2 to Python 3, ensuring compatibility so that it works on both Python 2 and Python 3. One of the problems I encountered is that Python 2 requires import ConfigParser, while Python 3 requires import configparser.

The functionality of the new version of the your project futurize, does not solve this problem.

On your project website (https://python-future.org/imports.html), it is stated:

Note that, as of v0.16.0, python-future no longer includes an alias for the configparser module because a full backport exists (see https://pypi.org/project/configparser/).

From this, I concluded that this issue is resolved by the tools of configparser's project.

After following your link to configparsers's project, I see:

This package is a backport of the refreshed and enhanced ConfigParser from later Python versions. To use the backport instead of the built-in version, simply import it explicitly as a backport:

`from backports import configparser`

To use the backport on Python 2 and the built-in version on Python 3, use the standard invocation:

`import configparser`

For detailed documentation consult the vanilla version at http://docs.python.org/3/library/configparser.html.

However, even after installing with pip install configparser, neither the code import configparser nor the code from backports import configparser works when running on Python 2.

Can you explain how to ensure configparser's compatibility between Python 2 and Python 3 without futurize's tools?

@edschofield
Copy link
Contributor

edschofield commented Mar 19, 2024 via email

@Pablitosi4
Copy link
Author

Try just replacing every instance of the string ConfigParser in your code with configparser

Thanks for such a quick response!
Unfortunately, your recommendation didn't help me. :(
Maybe I'm doing something wrong? I uninstalled confiparser and install again:

  • pip install configparser

Defaulting to user installation because normal site-packages is not writeable
Collecting configparser
Downloading configparser-6.0.1-py3-none-any.whl (19 kB)
Installing collected packages: configparser
Successfully installed configparser-6.0.1

  • pip list

Package Version
configparser 6.0.1
dbus-python 1.2.18
et-xmlfile 1.1.0
onboard 1.4.1
openpyxl 3.1.2
pip 22.1
pycairo 1.20.1
PyGObject 3.42.1
setuptools 59.6.0
six 1.16.0
urllib3 1.26.9
wheel 0.37.1

  • echo "import configparser" > file.py - # configparser

  • python3 --version

Python 3.10.4

  • python3 file.py ; echo -e "\nresult: $?" # py3; echo $?

result: 0

  • python --version

Python 2.7.18

  • python file.py ; echo -e "\nresult: $?" # py2; echo $?

Traceback (most recent call last):
File "file.py", line 1, in
import configparser
ImportError: No module named configparser

result: 1

  • echo "import ConfigParser" > file.py # ConfigParser

  • python file.py ; echo -e "\nresult: $?" # py2; echo $?

result: 0

  • python3 file.py ; echo -e "\nresult: $?" # py3; echo $?

_Traceback (most recent call last):
File "/home/user/file.py", line 1, in
import ConfigParser
ModuleNotFoundError: No module named 'ConfigParser'

result: 1_

Do I need to pip install configparser in both Python2 and Python3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants