Skip to main content

Module importer with its own namespace and reflection feature for python.

Project description

moduleimporter_python

Module importer with its own namespace and reflection feature for python

Introduction

This importer works like Java URLClassLoader that you can import modules have same name or different versions without conflict. When you do not want to import a module to global namespace sys.modules, it also works.

Every importer of this library will create its own namespace when initializing, and all modules imported by this importer will be injected into importer's namespace, rather than sys.modules.

The package also provide wrapper for python modules, methods, classes and attributes to implement reflection.

Two high level importers are provided, including PathModuleImporter as default and NsModuleImporter as optional if you have another implementation of namespace importer (refer to nsimport) installed .

  • PathModuleImporter uses this package's util module which re-implement importlib with namespace specific so that it has its own namespace when instance is created.

  • NsModuleImporter uses importlib as implementation but creates new namespace and bind it to _bootstrap and _bootstrap_external. It has its own namespace after _bootstrap is inintialized.

APIs

Available module can be checked in __init__.py, and the main functions to load modules can be checked in _PathModuleImporter of Importer.py and util.py. The functions are short enough to read.

Functions and classes of reflection can be checked in _wrapper.py.

Generally, you can use moduleimporter.PathModuleImporter(path) to initialize a importer. Path is a list where importer to find modules, and it works like sys.path. Path will be initialized as path + sys.path to allow to import other package.

importer.load_module(name_of_module) returns a wrapped module that supports reflection.

Access importer's attribute with importer.modules, importer.path and importer.meta_path etc.

Access wrapped module with importer.unwrap(to_unwrap_object) or wrapped.module.

Example

>>> import sys
>>> import moduleimporter as MI
>>> importer = MI.PathModuleImporter(['./'])
>>> importer
<moduleimporter.Importer.PathModuleImporter object at 0x7f976241de50>
>>> importer.load_module('codeop')
<moduleimporter._wrapper.PyModule object at 0x7f976222fb50>
>>> 'codeop' in sys.modules
False
>>> 'codeop' in importer.modules
True
>>> random_module = importer.load_module('random')
>>> random_module
<moduleimporter._wrapper.PyModule object at 0x7f976222fb50>
>>> rand_int = random_module.get_method('randint')
>>> rand_int
<moduleimporter._wrapper.PyMethod object at 0x7f97620fb550>
>>> rand_int.invoke(1, 10)
10
>>> rand_int.invoke(1, 10)
4
>>> random_module.get_attr('NV_MAGICCONST')
<moduleimporter._wrapper.PyAttr object at 0x7f9762107640>
>>> random_module.get_class('Random')
<moduleimporter._wrapper.PyClass object at 0x7f97620847c0>
>>> rand_int.get_parameter_names()
['a', 'b']

Notice

Every importer has its own namespace, and all modules loaded by now loading module will be injected to importer's namespace rather than global sys.modules.

Only test on python3.8, and you can run scripts in tests to check if the library works.

Core implementation to load module to namespace is in util. Importer initialization uses functools to bind a namespace to import function.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moduleimporter-1.2.1.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

moduleimporter-1.2.1-py2.py3-none-any.whl (12.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file moduleimporter-1.2.1.tar.gz.

File metadata

  • Download URL: moduleimporter-1.2.1.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.10

File hashes

Hashes for moduleimporter-1.2.1.tar.gz
Algorithm Hash digest
SHA256 ac6fb84f4ae3446e523f58ac572ff959964500b09ad12873673d66db82adb292
MD5 5c0bfae83c4d1c23db3494589368a811
BLAKE2b-256 c5a1b39ad95e7e92562b34f8b5d607514fc23653cb67481d09ee6a4d4623168d

See more details on using hashes here.

File details

Details for the file moduleimporter-1.2.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for moduleimporter-1.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 53cebc354a56b0dcc6d005449ccd061df3eed356495f61e129d29b1866e9a243
MD5 31fe975032d3a5a585ccced27e743293
BLAKE2b-256 2f9d73e817c27ea4020dc7b1b1280479c9c811e60b9948740a2672e5e8c5ad77

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page