PythonCharmers / python-future

Easy, clean, reliable Python 2/3 compatibility
http://python-future.org
MIT License
1.17k stars 291 forks source link

Compatibility of configparser with Python2 and Python3 #638

Open Pablitosi4 opened 3 months ago

Pablitosi4 commented 3 months ago

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 configparsernor 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 commented 3 months ago

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

Pablitosi4 commented 3 months ago

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:

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

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

Python 3.10.4

result: 0

Python 2.7.18

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

result: 1

result: 0

_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 configparserin both Python2 and Python3?