首页 > 解决方案 > 如何修复此 Python ModuleNotFoundError

问题描述

我试图弄清楚是什么导致这个名为 builder.py 的文件无法在 mac 上运行,即使它在 windows 上运行。代码:

import cffi
import glob
import platform

# relative to build dir
LIB_BASE = '../libs/'
# compiling libraries statically to get a single binary
EXTRA_SRC = [LIB_BASE + 'subhook/subhook.c']

pltsysname = {'Windows': 'win32', 'Darwin': 'osx', 'Linux': 'elf'}
pltsrc = pltsysname[platform.system()]
pltsrc = LIB_BASE + 'plthook/plthook_{}.c'.format(pltsrc)
# EXTRA_SRC.append(pltsrc)  # disabled until it is actually useful

LIBDIRS = []
if platform.system() == 'Windows':
LIBDIRS.append('../libs/SDL/lib/x86/')

CDEFS = 'generated internals SDL XDL subhook xternPython'.split()


def readfile(name):
with open(name, 'r') as f:
    content = f.read()
return content


def build():
ffibuilder = cffi.FFI()

for fname in CDEFS:
    ffibuilder.cdef(readfile('cdefs/{}.h'.format(fname)))

ffibuilder.embedding_api('uint32_t kickstart();')
ffibuilder.embedding_init_code(readfile('remote.py'))

ffibuilder.set_source(
    '_remote', readfile('cdefs/remote.c'), sources=EXTRA_SRC,
    libraries=['SDL2'], library_dirs=LIBDIRS,
    define_macros=[('SUBHOOK_STATIC', None)])

ffibuilder.compile(tmpdir='build', target='remote.bin')


if __name__ == '__main__':
build()

当我运行它时,我希望它能够运行,但它会出现以下错误:

Traceback (most recent call last):
File "/Users/alexanderlee/Desktop/sbpe-1.6.1/builder.py", line 1, in <module>
import cffi
ModuleNotFoundError: No module named 'cffi'
>>> 

我如何解决它?

标签: python

解决方案


这可能是因为您只安装cffi在 Windows 上,所以您可能还需要在 Mac 上安装它。

您可以遵循文档上的规则:

pip install cffi


推荐阅读