Skip to main content

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

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 localimport object alive, especially if you use from xx import yy imports.

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.path inside 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 localimport() constructor (using sys._getframe(1).f_globals['__file__']).
  • do_eggs – A boolean that indicates whether .egg files or directories found in the additional paths are added to sys.path.
  • do_pth – A boolean that indicates whether .pth files 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_autodisable parameter.

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 the localimport() 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

localimport-1.7.6.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

localimport-1.7.6-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file localimport-1.7.6.tar.gz.

File metadata

  • Download URL: localimport-1.7.6.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for localimport-1.7.6.tar.gz
Algorithm Hash digest
SHA256 f1485a67219d37f37a53047ba4f6104768590b3dd3ac1c6bd4a381251c76f289
MD5 316a565542ed92b278039d0247816650
BLAKE2b-256 4a515769b8a2ba0ded922d1607589b4d243dccb3f9f4c17cb39a3e938834cc9a

See more details on using hashes here.

File details

Details for the file localimport-1.7.6-py3-none-any.whl.

File metadata

  • Download URL: localimport-1.7.6-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for localimport-1.7.6-py3-none-any.whl
Algorithm Hash digest
SHA256 bbb72fa67f911ac7960f37616b1cccee388a980b4ce05b37d5eeba2b4e952ab0
MD5 6648b37717be4f39ba8e1fa32f25ebbd
BLAKE2b-256 de9af5e392b3d2ef8a8dc2b29aacc07d8c489ac4df0a11534fca7f59d3e00676

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page