find_module
및 load_module
은 모두 사용되지 않습니다. find_spec
및 (create_module
및 exec_module
) 모듈로 각각 전환해야합니다. 자세한 내용은 importlib
documentation을 참조하십시오.
또한 MetaPathFinder
또는 PathEntryFinder
을 호출 할 시스템이 다른지 여부를 검사해야합니다. 즉, 메타 경로 파인더가 먼저 실행되고 내장 모듈을 오버라이드 할 수 있지만 경로 입력 찾기는 sys.path
에있는 모듈에 대해 특별히 작동합니다.
다음은 전체 수입 기계를 교체하려는 매우 기본적인 수입업자입니다. 기능 (find_spec
, create_module
및 exec_module
)을 사용하는 방법을 보여줍니다.
import sys
import os.path
from importlib.abc import Loader, MetaPathFinder
from importlib.util import spec_from_file_location
class MyMetaFinder(MetaPathFinder):
def find_spec(self, fullname, path, target=None):
if path is None or path == "":
path = [os.getcwd()] # top level import --
if "." in fullname:
*parents, name = fullname.split(".")
else:
name = fullname
for entry in path:
if os.path.isdir(os.path.join(entry, name)):
# this module has child modules
filename = os.path.join(entry, name, "__init__.py")
submodule_locations = [os.path.join(entry, name)]
else:
filename = os.path.join(entry, name + ".py")
submodule_locations = None
if not os.path.exists(filename):
continue
return spec_from_file_location(fullname, filename, loader=MyLoader(filename),
submodule_search_locations=submodule_locations)
return None # we don't know how to import this
class MyLoader(Loader):
def __init__(self, filename):
self.filename = filename
def create_module(self, spec):
return None # use default module creation semantics
def exec_module(self, module):
with open(self.filename) as f:
data = f.read()
# manipulate data some way...
exec(data, vars(module))
def install():
"""Inserts the finder into the import machinery"""
sys.meta_path.insert(0, MyMetaFinder())
다음은 더 많은 수입 기계를 재사용하려고하는 약간 더 민감한 버전입니다. 따라서 모듈 원본을 가져 오는 방법 만 정의하면됩니다.
import sys
from os.path import isdir
from importlib import invalidate_caches
from importlib.abc import SourceLoader
from importlib.machinery import FileFinder
class MyLoader(SourceLoader):
def __init__(self, fullname, path):
self.fullname = fullname
self.path = path
def get_filename(self, fullname):
return self.path
def get_data(self, filename):
"""exec_module is already defined for us, we just have to provide a way
of getting the source code of the module"""
with open(filename) as f:
data = f.read()
# do something with data ...
# eg. ignore it... return "print('hello world')"
return data
loader_details = MyLoader, [".py"]
def install():
# insert the path hook ahead of other path hooks
sys.path_hooks.insert(0, FileFinder.path_hook(loader_details))
# clear any loaders that might already be in use by the FileFinder
sys.path_importer_cache.clear()
invalidate_caches()
'imp.new_module' 문서를 보면, 3.4 버전부터는 더 이상 사용되지 않을 것입니다 : 대신 types.ModuleType을 사용하십시오. 문제가 해결되지 않습니까? –
imp.modules를이 방법으로 교체해야한다는 것을 알았지 만 문서에서 modulelfromspec (importlib의)을 사용하도록 명시하고 있습니다. imp에서 3 가지 방법을 사용하여 사용자 지정 후크 가져 오기를 수행하고 importlib에 해당하는 것을 찾아야합니다. –