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

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 -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


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.6.1a2.tar.gz (7.1 kB view hashes)

Uploaded Source

Supported by

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