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

Python 3.12 removes imp module #625

Closed
odie5533 opened this issue Oct 8, 2023 · 4 comments · Fixed by #632
Closed

Python 3.12 removes imp module #625

odie5533 opened this issue Oct 8, 2023 · 4 comments · Fixed by #632

Comments

@odie5533
Copy link
Contributor

odie5533 commented Oct 8, 2023

Future relies on the imp module in standard_library, used for things like from future.standard_library import hooks. In Python 3.12, the module has been completely removed. Future needs to switch to use importlib.

# future/standard_library/__init__.py
        name = bits[0]
        module_info = imp.find_module(name, path)
        return imp.load_module(name, *module_info)
    from future.standard_library import hooks
  File "/home/odie5533/test/venv/lib/python3.12/site-packages/future/standard_library/__init__.py", line 65, in <module>
    import imp
ModuleNotFoundError: No module named 'imp'

Workaround

If your code is like this

# Python 2 and 3: alternative 2
from future.standard_library import hooks

with hooks():
    from urllib.parse import urlparse, urlencode
    from urllib.request import urlopen, Request
    from urllib.error import HTTPError

You can rewrite it using the try-except form in the Cheat Sheet.

# Python 2 and 3: alternative 4
try:
    from urllib.parse import urlparse, urlencode
    from urllib.request import urlopen, Request
    from urllib.error import HTTPError
except ImportError:
    from urlparse import urlparse
    from urllib import urlencode
    from urllib2 import urlopen, Request, HTTPError
@odie5533
Copy link
Contributor Author

odie5533 commented Oct 11, 2023

Tried my hand at a PR but I can't dip into another codebase right now and I'm unsure of the state of some of the tests.

@amotl
Copy link

amotl commented Oct 14, 2023

Thank you for submitting GH-626. We are also running into this over at mqttwarn.

rcrdnalor added a commit to rcrdnalor/mythtv that referenced this issue Jan 8, 2024
Python 3.12 removes the imp module but python-future v0.18.3
uses it, see PythonCharmers/python-future#625
Since 4b5eac8, python-future is not needed anymore.
rcrdnalor added a commit to MythTV/mythtv that referenced this issue Jan 8, 2024
Python 3.12 removes the imp module but python-future v0.18.3
uses it, see PythonCharmers/python-future#625
Since 4b5eac8, python-future is not needed anymore.
@liquidsec
Copy link

liquidsec commented Jan 17, 2024

Any chance for a real fix and not just a workaround? I've got a project that uses this as a dependency 2 levels down, causing me to be python 3.12 incompatible.

richardpenman/whois#188

@nsoranzo
Copy link
Contributor

nsoranzo commented Feb 1, 2024

@liquidsec Have a look at #632 .

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

Successfully merging a pull request may close this issue.

4 participants