Skip to content

Python 3.12 removes imp module #625

Description

@odie5533

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__.pyname=bits[0]
module_info=imp.find_module(name, path)
returnimp.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 2fromfuture.standard_libraryimporthookswithhooks():
fromurllib.parseimporturlparse, urlencodefromurllib.requestimporturlopen, Requestfromurllib.errorimportHTTPError

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

# Python 2 and 3: alternative 4try:
fromurllib.parseimporturlparse, urlencodefromurllib.requestimporturlopen, Requestfromurllib.errorimportHTTPErrorexceptImportError:
fromurlparseimporturlparsefromurllibimporturlencodefromurllib2importurlopen, Request, HTTPError

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions