Skip to main content

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.

Metadata

Release files for moduleimporter 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for moduleimporter 1.2.1
File Size Uploaded
moduleimporter-1.2.1.tar.gz 14.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for moduleimporter 1.2.1
File Interpreter ABI Platform
moduleimporter-1.2.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 26.9 kB

Release files / moduleimporter-1.2.1.tar.gz

Download URL moduleimporter-1.2.1.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ac6fb84f4ae3446e523f58ac572ff959964500b09ad12873673d66db82adb292
BLAKE2b-256 checksum
How to use checksums
c5a1b39ad95e7e92562b34f8b5d607514fc23653cb67481d09ee6a4d4623168d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.8.10

Release files / moduleimporter-1.2.1-py2.py3-none-any.whl

Download URL moduleimporter-1.2.1-py2.py3-none-any.whl
Size 12.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
53cebc354a56b0dcc6d005449ccd061df3eed356495f61e129d29b1866e9a243
BLAKE2b-256 checksum
How to use checksums
2f9d73e817c27ea4020dc7b1b1280479c9c811e60b9948740a2672e5e8c5ad77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.8.10

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page