Isolated import of Python Modules
Project description
localimport allows you to import Python modules in an isolated environment, preserving the global importer state.
Features
Emulates an isolated environment for Python module imports
Evaluates *.pth files
Compatible with pkg_resources namespaces
Mocks pkgutil.extend_path() to support zipped Python eggs
Example
Given your Python script, application or plugin comes with a directory that contains modules for import, you can use localimport to keep the global importer state clean.
app.py res/modules/ some_package/ __init__.py
# app.py
with localimport('res/modules') as _importer:
import some_package
assert 'some_package' not in sys.modules
**Important**: You must keep the reference to the ``localimport``
object alive, especially if you use ``from xx import yy`` imports.
Usage
Pre-minified versions of localimport can be found in this Gist. Of course you can minify the code by yourself, for example using the nr command-line tools.
nr py.blob localimport.py -cm > localimport-gzb64-w80.py
Depending on your application, you may want to use a bootstrapper entry point.
# @@@ minified localimport here @@@
with localimport('.') as _importer:
_importer.disable('my_application_package')
from my_application_package.__main__ import main
main()
Copyright © 2018 Niklas Rosenstein
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.