【已解决】在macOS上安装thefuck运行失败(ModuleNotFoundError, No module named 'imp')

修复了问题:ModuleNotFoundError: No module named ‘imp’ (python 3.12.0)

修复方法

如果你在使用 Python 3.12 及以上版本,这个问题非常好修复。

因为imp在 Python 3.12 被移除了,而目前 Github 上的版本并没有对此作出适配,所以需要手动调整。受到影响的文件总共有两个:

/opt/homebrew/lib/python3.13/site-packages/thefuck/types.py

/opt/homebrew/lib/python3.13/site-packages/thefuck/conf.py

你需要手动把这个文件中的

from imp import load_source

替换为

import importlib.util
import importlib.machinery

并在代码中加入:

def load_source(modname, filename):
    loader = importlib.machinery.SourceFileLoader(modname, filename)
    spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
    module = importlib.util.module_from_spec(spec)
    # The module is always executed and not cached in sys.modules.
    # Uncomment the following line to cache the module.
    # sys.modules[module.__name__] = module
    loader.exec_module(module)
    return module

即可解决问题。

效果如图:

image

亲测有效:

image

项目地址

nvbn/thefuck