localimport allows you to import Python modules in an
isolated environment, preserving the global importer state.
- Emulates an isolated environment for Python module imports
- Compatible with
pkgutil.extend_path()to support zipped Python eggs
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
localimportobject alive, especially if you use
from xx import yyimports.
In most cases it would not make sense to use
localimport as a Python module
when you actually want to import Python modules since the import of the
localimport module itself would not be isolated.
The solution is to use the
localimport source code directly in your
application code. Usually you will use a minified version.
nr py.blob localimport.py -cme localimport > localimport-gzb64-w80.py
Depending on your application, you may want to use a bootstrapper entry point.
# @@@ minified localimport here @@@ with localimport('.') as _importer: from my_application_package.__main__ import main main()
localimport(path, parent_dir=None, do_eggs=True, do_pth=True, do_autodisable=True)
A context manager that creates an isolated environment for importing Python modules. Once the context manager exits, the previous global state is restored.
Note that the context can be entered multiple times, but it is not recommended generally as the only case where you would want to do that is inside a piece of code that gets executed delayed (eg. a function) which imports a module, and building the isolated environment and restoring to the previous state has some performance impacts.
Also note that the context will only remove packages on exit that have actually been imported from the list of paths specified in the path argument, but not modules from the standard library, for example.
- path – A list of paths that are added to
sys.pathinside the context manager. Can also be a single string. If one or more relative paths are passed, they are treated relative to the parent_dir argument.
- parent_dir – A path that is concatenated with relative paths passed to the path argument. If this argument is omitted or
None, it will default to the parent directory of the file that called the
- do_eggs – A boolean that indicates whether
.eggfiles or directories found in the additional paths are added to
- do_pth – A boolean that indicates whether
.pthfiles found in the additional paths will be evaluated.
- do_autodisable – A boolean that indicates that
localimport.autodisable()should be called automatically be the context manager.
Changed in 1.7 Added
localimport.discover()to automatically detect modules that could be imported from the paths in the importer context and calls #disable on all of them.
New in 1.7
Disable one or more modules by moving them from the global module cache (
sys.modules) to a dictionary of temporary hidden modules in the isolated environment. Once the
localimport()context manager exits, these modules will be restored. Does nothing when a module does not exist.
- modules – A list of module names or a single module name string.
A shorthand for
New in 1.7
Copyright © 2018 Niklas Rosenstein