Skip to main content

Reimport a module in Python3

Project description

Reimport-ng

Reimport-ng is a fork of the original reimport module by Peter Shinners. The original module was last updated in 2014 and is not compatible with Python 3.8+. This fork aims to update the module to work with Python 3.8+ and to fix any bugs that may have been present in the original module.

This module intends to be a full featured replacement for Python's reload function. It is targeted towards making a reload that works for Python plugins and extensions used by longer running applications.

Reimport currently supports Python 3.8+.

By its very nature, this is not a completely solvable problem. The goal of this module is to make the most common sorts of updates work well. It also allows individual modules and package to assist in the process. A more detailed description of what happens is on the Wiki page.

Quick Docs

There are two functions in the API.

def reimport(*modules):
    """Reimport python modules. Multiple modules can be passed either by
        name or by reference. Only pure python modules can be reimported."""
    return None

def modified(path=None):
    """Find loaded modules that have changed on disk under the given path.
        If no path is given then all modules are searched."""
    return list_of_strings 

Related

There have been previous attempts at python reimporting. Most are incomplete or frightening, but several of them are worth a closer look.

  • Livecoding is one of the more complete, it offers a special case directory tree of Python modules that are treated as live files.
  • mod_python has implemented a similar reloading mechanism. The module reloading itself may be difficult to use outside mod_python's environment.
  • xreload The python source itself comes with a minimal extended reload.
  • globalsub Replace and restore objects with one another globally.

Overview of the reimport process

The reimport process is handled in several steps.

  • A list of modules and packages are given to be reimported.
  • For each module, we check all parent packages for a package_reimport value. If the value is True we will reimport the entire package, instead of just the submodule.
  • Build a unique set of final modules and packages to reimport. Sort them by package depth order.
  • Check each module for SyntaxError and early exception out.
  • Move all packages to be reloaded out of sys.modules and hang onto them.
  • Reimport modules one at a time. Check to make sure it hasn't already been imported from a parent package being reimported.
    • If module added values to all that are missing, AttributeError is raised and reimports are rolled back.
  • Find reimported callback and pass the old module reference as an argument
    • If callback returns False, do not perform the rejigger for that module
    • Exceptions from the callback are redirected to traceback.print_exc
  • Find parent packages that haven't been reimported that appear to import * (change for 1.1)
    • Push exported symbols from children into these parents (change for 1.1)
  • Begin rejigger process for each module imported
    • Match old objects to new objects by name
    • Transmute classes and functions in the module from old to new
    • Switch references from the old object to the new
    • For lists, sets, and dictionaries this isn't tricky.
    • For tuples it is trickier, but an attempt is made to build a new tuple, and swap references to the tuple itself.
    • Classes that derive from the old object have their bases modified.
    • Instance have their class swapped
    • Remove references to old objects that have no matching named object
    • Similar process to the above reference switching
    • Note, this doesn't seem to find bound methods to a method that gets dropped
      • My first guess is that the gc doesn't track bound methods? (surprising)

Credits

Reimport was written by Peter Shinners. The original module was last updated in 2014.

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

reimport_ng-1.5.1.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

reimport_ng-1.5.1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file reimport_ng-1.5.1.tar.gz.

File metadata

  • Download URL: reimport_ng-1.5.1.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for reimport_ng-1.5.1.tar.gz
Algorithm Hash digest
SHA256 434a661fe4967ae89ce618e70fc80723c26c883f1f78aae40e9c5f7a56bb86b1
MD5 8399079ccc004c476cb225e2364ab52d
BLAKE2b-256 7fc2e1a1437f9e16229d4fa2e637d31b30f0c1aa11d098fefe60e2c90cc81977

See more details on using hashes here.

File details

Details for the file reimport_ng-1.5.1-py3-none-any.whl.

File metadata

  • Download URL: reimport_ng-1.5.1-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for reimport_ng-1.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e15971fa7d6eec6954929cac0a6fe6a378c5fa41c94e6460beda89c52e2a09c3
MD5 a8669b7043fc266d5ff037ccab4f25a4
BLAKE2b-256 345e5c06d5929aeafdc6df6034969a75f761d12e31d82819d7b8e408d5d21bf8

See more details on using hashes here.

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