我对此失去了理智。它已经过了一年的半年,我仍然试图计算python相对库导入。
我正在使用NPP_EXEC插件运行python2.7和python3.5解释器和notepad ++,并且在我理解python / lib文件夹中的每个库之前我尝试不使用外部库,当然我自己的库不计算
我用谷歌搜索并研究了这个话题,大多数响应是“使用-m / -c开关”,这很好,但我不能在脚本中使用这些开关,只能从控制台使用。
几个月后,我意识到我需要 init 和主文件在文件夹中,我已经做到了但仍然有很多很多的ImportErrors不断出现,这是不可理解的解决这个错误。
我打开了pkgutil.py文件,看到iter_importers函数中有下一个代码:
python27
def iter_importers(fullname=""):
"""Yield PEP 302 importers for the given module name
If fullname contains a '.', the importers will be for the package
containing fullname, otherwise they will be importers for sys.meta_path,
sys.path, and Python's "classic" import machinery, in that order. If
the named module is in a package, that package is imported as a side
effect of invoking this function.
Non PEP 302 mechanisms (e.g. the Windows registry) used by the
standard import machinery to find files in alternative locations
are partially supported, but are searched AFTER sys.path. Normally,
these locations are searched BEFORE sys.path, preventing sys.path
entries from shadowing them.
For this to cause a visible difference in behaviour, there must
be a module or package name that is accessible via both sys.path
and one of the non PEP 302 file system mechanisms. In this case,
the emulation will find the former version, while the builtin
import mechanism will find the latter.
Items of the following types can be affected by this discrepancy:
imp.C_EXTENSION, imp.PY_SOURCE, imp.PY_COMPILED, imp.PKG_DIRECTORY
"""
if fullname.startswith('.'):
raise ImportError("Relative module names not supported")
if '.' in fullname: ## this is what got me to depression stage
# Get the containing package's __path__
pkg = '.'.join(fullname.split('.')[:-1])
if pkg not in sys.modules:
__import__(pkg)
path = getattr(sys.modules[pkg], '__path__', None) or []
else:
for importer in sys.meta_path:
yield importer
path = sys.path
for item in path:
yield get_importer(item)
if '.' not in fullname:
yield ImpImporter()
python35
def iter_importers(fullname=""):
"""Yield PEP 302 importers for the given module name
If fullname contains a '.', the importers will be for the package
containing fullname, otherwise they will be all registered top level
importers (i.e. those on both sys.meta_path and sys.path_hooks).
If the named module is in a package, that package is imported as a side
effect of invoking this function.
If no module name is specified, all top level importers are produced.
"""
if fullname.startswith('.'):
msg = "Relative module name {!r} not supported".format(fullname)
raise ImportError(msg)
if '.' in fullname:
# Get the containing package's __path__
pkg_name = fullname.rpartition(".")[0]
pkg = importlib.import_module(pkg_name)
path = getattr(pkg, '__path__', None)
if path is None:
return
else:
yield from sys.meta_path
path = sys.path
for item in path:
yield get_importer(item)
因为在iter_imports统计信息中的pkgutil.py文件中,如果fullname以'。'开头。它会引发错误,而不会计算任何它接触到我的python语言中不存在相对导入(这是一种耻辱),但后来我感到困惑,因为python官方页面声明相对导入是可能的PEP 328,但是如何它完成了逃避官方页面,它只描述了没有任何结构文件的过程。
如果有人可以使用python的相对导入我将非常感谢sed人可以做下一步:
描述相对导入的最低要求:
我这样做是因为我希望如果有人可以回答这个问题它会帮助像我这样的任何新手以及刚刚开始使用python的任何其他人使用python的这个很棒的功能(如果它存在)并因此阻止这个问题来自被重复。