模块 'importlib' 没有属性 'util'

module 'importlib' has no attribute 'util'

我在我的树莓派 3.7 上使用 Python 3.7,我得到一个错误,我的模块 importlib 没有 util,你能告诉我如何解决这个问题吗?

如果我这样做:

pi@raspberrypi:~ $ python3
>>> import importlib
>>> dir(importlib)

我得到:

['_RELOADING', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__import__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_bootstrap', '_bootstrap_external', '_imp', '_r_long', '_w_long', 'find_loader', 'import_module', 'invalidate_caches', 'reload', 'sys', 'types', 'warnings']

所以 :

>>> importlib.util

returns:

回溯(最近调用最后): 文件“”,第 1 行,位于 AttributeError: 模块 'importlib' 没有属性 'util'

我尝试使用 Python 3.8,但我得到了相同的结果 提前谢谢你

你需要做 import importlib.util:

>>> import importlib.util
>>> dir(importlib.util)
['LazyLoader', 'MAGIC_NUMBER', '_LazyModule', '_RAW_MAGIC_NUMBER', '__builtins__',
'__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', 
'__spec__', '_find_spec', '_find_spec_from_path', '_imp', '_module_to_load',
'_resolve_name', 'abc', 'cache_from_source', 'contextmanager', 'decode_source',
'find_spec', 'functools', 'module_for_loader', 'module_from_spec', 'resolve_name',
'set_loader', 'set_package', 'source_from_cache', 'source_hash',
'spec_from_file_location', 'spec_from_loader', 'sys', 'types', 'warnings']
>>> 

相关文档:importlib.util