Skip to main content

With creating individual namespaces for python import mechanism, we can use NsImporter to load modules without conflict, loading modules to different namespaces (not inject them into global namespace).

All modules loaded by NsImporters will be added to NsImporter’s own namespace (access with self.sys), and sys.module contains no them.

So we can import our modules without conflict such as modules with same name, different packages versions or other situations we do not want loaded modules present in sys.modules.

This library is modified from importlib of python3.8, but there are only 2 differences between nsimport and importlib:

  • __init__.py of importlib is edited to be nsimporter_internal.py by creating a class type to implement namespace initializing before being used to import modules. The class is initialized by set namespace’s sys to parameter sys passed to it.

  • new __init__.py for whole library.

APIs

All available defined APIs can be found in __init__.py and nsimporter_internal.py.

But it is disencouraged to directly use APIs except nsimport.get_NsImporter(path: list) which returns a new NsImporter initialized with a copied sys module.

If you know exactly what module sys do when interpreter imports modules, you can create your own namespace and pass it to nsimport.NsImporter(sys) and it will be use to initialize the importer.

Different NsImporters have different namespaces (if different sys modules are passed to it), so it is feasible to use 2 or more importer at the same time.

You can access namespace’s sys module by accessing instance’s member sys using self.sys, and access namespace’s importlib bootstrap using self._bootstrap and self._bootstrap_external.

Parameters

  • nsimport.get_NsImporter(path: list)

    path is a python list that indicates folders PathFinder use to find modules. Inside the importer, self.sys.path will be assigned path + sys.path, and it acts as sys.path in importer’s own namespace.

  • nsimport.NsImporter(sys: module)

    sys is a module with attributes sys.path, sys.modules, sys.meta_path, sys.path_hooks and sys.path_importer_cache = {}. All of these attributes should be independent to sys of global namespace, otherwise the importer will still add module to global sys.modules.

    nsimport.get_NsImporter provides a convenient way to create needed sys and passes it to NsImporter to create instance.

Example

>>> import nsimport
>>> importer = nsimport.get_NsImporter([])
>>> importer
<nsimport.nsimporter_internal.implib object at 0x7fae0568fdc0>
>>> importer.import_module('os')
<module 'os' from '/usr/local/lib/python3.8/os.py'>
>>> importer.import_module('codeop')
<module 'codeop' from '/usr/local/lib/python3.8/codeop.py'>
>>> 'codeop' in sys.modules
False
>>> 'codeop' in importer.sys.modules
True

Notice

For every NsImporter has its own namespace, all modules loaded by now loading module will be injected to NsImporter’s sys.modules but global sys.modules.

Only test on python3.8, and you can run script import_tests.py to check if the library works.

Though try best to make sure the modules in sys.modules are isolated to namespace’s sys.modules, modules imported before importing _bootsrap_external may still use global sys in its namespace. Not essential modules before _bootstrap_external are removed with function convert_modules_sys in nsimporter_internal.py, and other modules will be copied and set new sys module. Unit test test_issue_pre_loaded_modules and test_issue_pre_loaded_modules_solved_no_leakage are for this problem. Another implementation does not face this problem.

Details of implementation are in function nsimporter_internal.implib.__init__ which replaces sys module of namespaces of _bootstrap functions with its own sys.

Advance

Another implementation of namespace specific importer is in moduleimporter, and it provide more general apis, wrapper objects, and reflection mechanism for python modules.

Metadata

Release files for nsimport 0.1.3

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

Source distribution (sdist)

Source distribution for nsimport 0.1.3
File Size Uploaded
nsimport-0.1.3.tar.gz 48.7 kB Details

Built distribution (wheel)

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

Total release size: 98.7 kB

Release files / nsimport-0.1.3.tar.gz

Download URL nsimport-0.1.3.tar.gz
Size 48.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4e3f39e5017f875fcc6e673fa13816fd5cc2436d1225490cafd904b1bc5a8fff
BLAKE2b-256 checksum
How to use checksums
e28c5fe9154c3191e77b7149d55b1e6c9e4f87e3d98166c49d2f50ebfdfe9757
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.16

Release files / nsimport-0.1.3-py2.py3-none-any.whl

Download URL nsimport-0.1.3-py2.py3-none-any.whl
Size 50.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
0f6c7e6283fcf9e1b20350f4a6006e565d5fac46aa5553538ddebdf6900c9996
BLAKE2b-256 checksum
How to use checksums
4d57b6b5c7c206bf3e377aafba652dfbc4f0b5297220ce5776a93827af81d7c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.16

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

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