Python代码:
class Importer:
from importlib import __import__, reload
from sys import modules
libname = ""
import_count = 0
module = None
def __init__(self, name):
self.libname = name
self.import_count = 0
def importm(self):
if self.libname not in self.modules:
self.module = __import__(self.libname)
else:
print("must reload")
self.module = self.reload(self.module)
self.import_count += 1
# test out Importer
importer = Importer("module")
importer.importm() # prints Hello
importer.importm() # prints Hello
importer.importm() # prints Hello (again)
print(importer.import_count)
上面的Python(3.8.1)代码位于OnlineGDB上,如果运行,它将给出一个错误:
TypeError: reload() takes 1 positional argument but 2 were given
当我importlib
用Python打开该库时,我看到以下内容:
# ... (previous code; unnecessary)
_RELOADING = {}
def reload(module): ## this is where reload is defined (one argument)
"""Reload the module and return it.
The module must have been successfully imported before.
"""
if not module or not isinstance(module, types.ModuleType): ## check for type of module
raise TypeError("reload() argument must be a module")
try:
name = module.__spec__.name
except AttributeError:
name = module.__name__
if sys.modules.get(name) is not module: ## other code you (probably) don't have to care about
msg = "module {} not in sys.modules"
raise ImportError(msg.format(name), name=name)
if name in _RELOADING:
return _RELOADING[name]
_RELOADING[name] = module
try:
parent_name = name.rpartition('.')[0]
if parent_name:
try:
parent = sys.modules[parent_name]
except KeyError:
msg = "parent {!r} not in sys.modules"
raise ImportError(msg.format(parent_name),
name=parent_name) from None
else:
pkgpath = parent.__path__
else:
pkgpath = None
target = module
spec = module.__spec__ = _bootstrap._find_spec(name, pkgpath, target)
if spec is None:
raise ModuleNotFoundError(f"spec not found for the module {name!r}", name=name)
_bootstrap._exec(spec, module)
# The module may have replaced itself in sys.modules!
return sys.modules[name]
finally:
try:
del _RELOADING[name]
except KeyError:
pass
# ... (After code; unnecessary)
所有双井号(##)注释都是我的
很明显,reload
DOES具有1个参数,并且检查该参数是否为模块。在OGDB(OnineGDB)代码中,我只传递了一个参数(很确定),并且它的类型是module(最有可能)。如果我删除该参数(可以编辑OGDB),它将给出:
TypeError: reload() argument must be module
因此出于某种原因,Python一直认为我的争论比实际要多。使它起作用的唯一方法是编辑importlib
文件以使其reload
具有两个参数(不是一个好主意)。
我尝试运行PDB,没有帮助。
有人能发现明显不对的地方吗,例如实际上有两个论点?
我需要做的是将import
s放在类之外,以使其正常工作。这是新的OGDB。致谢@ L3viathan。代码如下:
from importlib import __import__, reload
from sys import modules
class Importer:
libname = ""
import_count = 0
module = None
def __init__(self, name):
self.libname = name
self.import_count = 0
def importm(self):
if self.libname not in modules:
self.module = __import__(self.libname)
else:
print("must reload")
self.module = reload(self.module)
self.import_count += 1
# test out Importer
importer = Importer("module")
importer.importm() # prints Hello
importer.importm() # prints Hello
importer.importm() # prints Hello (again)
print(importer.import_count)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句