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
*.pthfiles - Compatible with
pkg_resourcesnamespaces - Mocks
pkgutil.extend_path()to support zipped Python eggs
Check out the localimport Documentation.
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
localimportobject alive, especially if you usefrom xx import yyimports.
Usage
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.
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 -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()
API
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.
Parameters
- 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 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 that
localimport.autodisable()should be called automatically be the context manager.Changed in 1.7 Added
do_autodisableparameter.
localimport.autodisable()
Uses
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
localimport.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.Parameters
- modules – A list of module names or a single module name string.
localimport.discover()
A shorthand for
pkgutil.walk_packages(importer.path).New in 1.7
Copyright © 2018 Niklas Rosenstein
Metadata
Release files for localimport 1.7.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| localimport-1.7.6.tar.gz | 8.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| localimport-1.7.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.8 kB
Release files / localimport-1.7.6.tar.gz
| Download URL | localimport-1.7.6.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f1485a67219d37f37a53047ba4f6104768590b3dd3ac1c6bd4a381251c76f289
|
|
BLAKE2b-256 checksum How to use checksums |
4a515769b8a2ba0ded922d1607589b4d243dccb3f9f4c17cb39a3e938834cc9a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2
|
Release files / localimport-1.7.6-py3-none-any.whl
| Download URL | localimport-1.7.6-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bbb72fa67f911ac7960f37616b1cccee388a980b4ce05b37d5eeba2b4e952ab0
|
|
BLAKE2b-256 checksum How to use checksums |
de9af5e392b3d2ef8a8dc2b29aacc07d8c489ac4df0a11534fca7f59d3e00676
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2
|
Security for localimport 1.7.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.