Package: localimport
localimportallows you to import Python modules in anisolated environment, preserving the global importer state.FeaturesEmulates an isolated environment for Python module importsEvaluates*.pthfilesCompatible withpkg_resourcesnamespacesMockspkgutil.extend_path()to support zipped Python eggsCheck out thelocalimport Documentation.ExampleGiven 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.pywithlocalimport('res/modules')as_importer:importsome_packageassert'some_package'notinsys.modulesImportant: You must keep the reference to thelocalimportobject alive,
especially if you usefrom xx import yyimports.UsageIn most cases it would not make sense to uselocalimportas a Python module
when you actually want to import Python modules since the import of thelocalimportmodule itself would not be isolated.The solution is to use thelocalimportsource code directly in your
application code. Usually you will use a minified version.Pre-minified versions oflocalimportcan be found in thisGist.
Of course you can minify the code by yourself, for example using thenrcommand-line tools.nr py.blob localimport.py -cme localimport > localimport-gzb64-w80.pyDepending on your application, you may want to use a bootstrapper entry point.# @@@ minified localimport here @@@withlocalimport('.')as_importer:frommy_application_package.__main__importmainmain()APIlocalimport(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 thepathargument, but not modules from the standard library, for example.Parameterspath– A list of paths that are added tosys.pathinside the
context manager. Can also be a single string. If one or more relative
paths are passed, they are treated relative to theparent_dirargument.parent_dir– A path that is concatenated with relative paths passed
to thepathargument. If this argument is omitted orNone, it will
default to the parent directory of the file that called thelocalimport()constructor (usingsys._getframe(1).f_globals['__file__']).do_eggs– A boolean that indicates whether.eggfiles or
directories found in the additional paths are added tosys.path.do_pth– A boolean that indicates whether.pthfiles found
in the additional paths will be evaluated.do_autodisable– A boolean that indicates thatlocalimport.autodisable()should be called automatically be the context manager.Changed in 1.7Addeddo_autodisableparameter.localimport.autodisable()Useslocalimport.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.7localimport.disable(modules)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 thelocalimport()context manager exits, these modules
will be restored. Does nothing when a module does not exist.Parametersmodules– A list of module names or a single module name string.localimport.discover()A shorthand forpkgutil.walk_packages(importer.path).New in 1.7Copyright © 2018 Niklas Rosenstein