本文整理汇总了Python中pkgutil.extend_path方法的典型用法代码示例。如果您正苦于以下问题:Python pkgutil.extend_path方法的具体用法?Python pkgutil.extend_path怎么用?Python pkgutil.extend_path使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pkgutil
的用法示例。
在下文中一共展示了pkgutil.extend_path方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_nested
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import extend_path [as 别名]
def test_nested(self):
pkgutil_boilerplate = (
'import pkgutil; '
'__path__ = pkgutil.extend_path(__path__, __name__)')
self.create_module('a.pkg.__init__', pkgutil_boilerplate)
self.create_module('b.pkg.__init__', pkgutil_boilerplate)
self.create_module('a.pkg.subpkg.__init__', pkgutil_boilerplate)
self.create_module('b.pkg.subpkg.__init__', pkgutil_boilerplate)
self.create_module('a.pkg.subpkg.c', 'c = 1')
self.create_module('b.pkg.subpkg.d', 'd = 2')
sys.path.insert(0, os.path.join(self.basedir, 'a'))
sys.path.insert(0, os.path.join(self.basedir, 'b'))
import pkg
self.addCleanup(unload, 'pkg')
self.assertEqual(len(pkg.__path__), 2)
import pkg.subpkg
self.addCleanup(unload, 'pkg.subpkg')
self.assertEqual(len(pkg.subpkg.__path__), 2)
from pkg.subpkg.c import c
from pkg.subpkg.d import d
self.assertEqual(c, 1)
self.assertEqual(d, 2)
示例2: testGenClient_SimpleDocEmptyInit
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import extend_path [as 别名]
def testGenClient_SimpleDocEmptyInit(self):
with test_utils.TempDir() as tmp_dir_path:
gen_client.main([
gen_client.__file__,
'--init-file', 'empty',
'--infile', GetTestDataPath('dns', 'dns_v1.json'),
'--outdir', tmp_dir_path,
'--overwrite',
'--root_package', 'google.apis',
'client'
])
expected_files = (
set(['dns_v1_client.py', 'dns_v1_messages.py', '__init__.py']))
self.assertEquals(expected_files, set(os.listdir(tmp_dir_path)))
init_file = _GetContent(os.path.join(tmp_dir_path, '__init__.py'))
self.assertEqual("""\"""Package marker file.\"""
import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)
""", init_file)
示例3: create_init
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import extend_path [as 别名]
def create_init(self, pkgname):
dirname = tempfile.mkdtemp()
sys.path.insert(0, dirname)
pkgdir = os.path.join(dirname, pkgname)
os.mkdir(pkgdir)
with open(os.path.join(pkgdir, '__init__.py'), 'w') as fl:
fl.write('from pkgutil import extend_path\n__path__ = extend_path(__path__, __name__)\n')
return dirname
示例4: ExtendPath
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import extend_path [as 别名]
def ExtendPath(self):
self.path = pkgutil.extend_path(self.path, self.name)
if self.parent is not None:
self.parent.ExtendPath()
示例5: _declare_namespace
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import extend_path [as 别名]
def _declare_namespace(self, package_name):
'''
Mock for #pkg_resources.declare_namespace() which calls
#pkgutil.extend_path() afterwards as the original implementation doesn't
seem to properly find all available namespace paths.
'''
self.state['declare_namespace'](package_name)
mod = sys.modules[package_name]
mod.__path__ = pkgutil.extend_path(mod.__path__, package_name)
示例6: load_modules
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import extend_path [as 别名]
def load_modules(base_name, base_path):
"""
Imports all modules underneath `base_module` in the module tree.
Note that if submodules are located in different directory trees, you
need to use `pkgutil.extend_path` to make all the folders appear in
the module's `__path__`.
Returns
-------
list
All the modules in the base module tree.
"""
modules = []
for importer, module_name, _ in pkgutil.iter_modules(base_path):
full_module_name = '{}.{}'.format(base_name, module_name)
if full_module_name not in sys.modules:
module = importer.find_module(module_name).load_module(
full_module_name)
else:
module = sys.modules[full_module_name]
modules.append(module)
return modules
示例7: extend_path
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import extend_path [as 别名]
def extend_path(pth, name):
'''
Better implementation of #pkgutil.extend_path() which adds support for
zipped Python eggs. The original #pkgutil.extend_path() gets mocked by this
function inside the #localimport context.
'''
def zip_isfile(z, name):
name.rstrip('/')
return name in z.namelist()
pname = os.path.join(*name.split('.'))
zname = '/'.join(name.split('.'))
init_py = '__init__' + os.extsep + 'py'
init_pyc = '__init__' + os.extsep + 'pyc'
init_pyo = '__init__' + os.extsep + 'pyo'
mod_path = list(pth)
for path in sys.path:
if zipfile.is_zipfile(path):
try:
egg = zipfile.ZipFile(path, 'r')
addpath = (
zip_isfile(egg, zname + '/__init__.py') or
zip_isfile(egg, zname + '/__init__.pyc') or
zip_isfile(egg, zname + '/__init__.pyo'))
fpath = os.path.join(path, path, zname)
if addpath and fpath not in mod_path:
mod_path.append(fpath)
except (zipfile.BadZipfile, zipfile.LargeZipFile):
pass # xxx: Show a warning at least?
else:
path = os.path.join(path, pname)
if os.path.isdir(path) and path not in mod_path:
addpath = (
os.path.isfile(os.path.join(path, init_py)) or
os.path.isfile(os.path.join(path, init_pyc)) or
os.path.isfile(os.path.join(path, init_pyo)))
if addpath and path not in mod_path:
mod_path.append(path)
return [os.path.normpath(x) for x in mod_path]
示例8: __enter__
# 需要导入模块: import pkgutil [as 别名]
# 或者: from pkgutil import extend_path [as 别名]
def __enter__(self):
# pkg_resources comes with setuptools.
try:
import pkg_resources
nsdict = copy.deepcopy(pkg_resources._namespace_packages)
declare_namespace = pkg_resources.declare_namespace
pkg_resources.declare_namespace = self._declare_namespace
except ImportError:
nsdict = None
declare_namespace = None
# Save the global importer state.
self.state = {
'nsdict': nsdict,
'declare_namespace': declare_namespace,
'nspaths': {},
'path': sys.path[:],
'meta_path': sys.meta_path[:],
'disables': {},
'pkgutil.extend_path': pkgutil.extend_path,
}
# Update the systems meta path and apply function mocks.
sys.path[:] = self.path
sys.meta_path[:] = self.meta_path + sys.meta_path
pkgutil.extend_path = extend_path
# If this function is called not the first time, we need to
# restore the modules that have been imported with it and
# temporarily disable the ones that would be shadowed.
for key, mod in items(self.modules):
try: self.state['disables'][key] = sys.modules.pop(key)
except KeyError: pass
sys.modules[key] = mod
# Evaluate imports from the .pth files, if any.
for fn, lineno, stmt in self.pth_imports:
exec_pth_import(fn, lineno, stmt)
# Add the original path to sys.path.
sys.path += self.state['path']
# Update the __path__ of all namespace modules.
for key, mod in items(sys.modules):
if mod is None:
# Relative imports could have lead to None-entries in
# sys.modules. Get rid of them so they can be re-evaluated.
prefix = key.rpartition('.')[0]
if hasattr(sys.modules.get(prefix), '__path__'):
del sys.modules[key]
elif hasattr(mod, '__path__'):
self.state['nspaths'][key] = copy.copy(mod.__path__)
mod.__path__ = pkgutil.extend_path(mod.__path__, mod.__name__)
self.in_context = True
if self.do_autodisable:
self.autodisable()
return self