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.1.0.tar.gz (13.6 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.1.0-py2.py3-none-any.whl (12.3 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for moduleimporter-1.1.0.tar.gz
Algorithm Hash digest
SHA256 352cb71d1f5a3465046ab8ff42afbe7b9a1ad427315719eabbe6f673df8593bf
MD5 e4126f638365af43ea0ba07192a954a6
BLAKE2b-256 48399ebd835df3256181e2252fea2d68114f0cf85cdde95d0cfe2fdcb13dfe56

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for moduleimporter-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0c674aea5bc92f7b971c03af768e028da1f4bdc228457fb79aa9ef5c5a7c294f
MD5 1a6700f8a20438a77459f992f1b15fa4
BLAKE2b-256 307a544dffc191352cf6505af6713933783ee6dc9db39ffda9574d54c129d118

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