Python pkgutil.extend_path() Examples
The following are 12
code examples of pkgutil.extend_path().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pkgutil
, or try the search function
.
Example #1
Source File: test_pkgutil.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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)
Example #2
Source File: test_pkgutil.py From ironpython3 with Apache License 2.0 | 6 votes |
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)
Example #3
Source File: test_pkgutil.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
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)
Example #4
Source File: gen_client_test.py From apitools with Apache License 2.0 | 6 votes |
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)
Example #5
Source File: test_pkgutil.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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
Example #6
Source File: test_pkgutil.py From ironpython3 with Apache License 2.0 | 5 votes |
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
Example #7
Source File: test_pkgutil.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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
Example #8
Source File: finder.py From tensorlang with Apache License 2.0 | 5 votes |
def ExtendPath(self): self.path = pkgutil.extend_path(self.path, self.name) if self.parent is not None: self.parent.ExtendPath()
Example #9
Source File: localimport.py From c4ddev with MIT License | 5 votes |
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)
Example #10
Source File: misc.py From sync-engine with GNU Affero General Public License v3.0 | 5 votes |
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
Example #11
Source File: localimport.py From c4ddev with MIT License | 4 votes |
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]
Example #12
Source File: localimport.py From c4ddev with MIT License | 4 votes |
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