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.1b2.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: reimport_ng-1.5.1b2.tar.gz
  • Upload date:
  • Size: 13.7 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.1b2.tar.gz
Algorithm Hash digest
SHA256 25548479480164653ba0a008314fe8b7041945c34336a3bedcf6a0683805bd9a
MD5 b7cce1785cb021a56e5db7943d667ac0
BLAKE2b-256 8f46b98ed841d86fab747a3e524bc297c0bec144b85dfab3106612ac0be45e3a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for reimport_ng-1.5.1b2-py3-none-any.whl
Algorithm Hash digest
SHA256 8bbe968a554468d1f9fce8b1245f86bcefbcaff2d3b8720daa29c4d7fa2eb537
MD5 a3a4cb36805871b5190d46a35683d2a1
BLAKE2b-256 a93549eee8167d1d823e7a796b421937ddd875c110363b7d0efcb75d8f9f9330

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