Skip to main content

Automatically reload modified Python modules in notebooks and scripts.

Project description

Automatically reload modified Python modules in notebooks and scripts.

Overview

LiveImport eliminates the need to restart a notebook's Python server to reimport code under development in external files. Suppose you are maintaining symbolic math code in symcode.py, LaTex formatting utilities in printmath.py, and a simulator in simulator.py. In the first cell of a notebook, you might write

import liveimport
import numpy as np
import matplotlib.plot as plot

import symcode
from printmath import print_math, print_equations as print_eq
from simulator import *

liveimport.register(globals(),"""
import symcode
from printmath import print_math, print_equations as print_eq
from simulator import *
""", clear=True)

Then, whenever you run cells, LiveImport will reload any of symcode, printmath, and simulator that have changed since they were registered or last reloaded. LiveImport deems a module changed when its source file modification time changes.

LiveImport also updates imported module symbols. For example, if you modify printmath.py, LiveImport will reload printmath and bind print_math and print_eq in the global namespace to the new definitions. Similarly, if you update simulator.py, LiveImport will create or update bindings for every public symbol in simulator — that is, every symbol defined in the module that does not start with _, unless there is an __all__ property defined for the module, in which case it acts on only those symbols, just like from simulator import *.

Modules referenced by registered import statements are called tracked modules. LiveImport analyzes top-level import dependencies between tracked modules to ensure reloading is consistent with those dependencies. Suppose simulator imports symcode. Then, if you modify symcode.py, LiveImport reloads simulator after it reloads symcode even if simulator.py has not changed. If you do modify both, LiveImport takes care to reload symcode first.

Cell Magic

As an alternative to calling register(), LiveImport defines a cell magic %%liveimport which both executes the cell content and registers every top-level import statement. So, you could split the example cell above into two, making the second

%%liveimport --clear
import symcode
from printmath import print_math, print_equations as print_eq
from simulator import *

No register() call is required. Unfortunately, Visual Studio Code and possibly other environments do not analyze magic cell content, making %%liveimport use awkward. However, there is a workaround: calling hidden_cell_magic(enabled=True) causes #_%%liveimport lines at the beginning of cells to act just like %%liveimport when the cell is run, and since #_%%liveimport is a Python comment, Visual Studio Code and other environments do analyze the content.

A complete cell magic example equivalent to the first begins with cell

import liveimport
import numpy as np
import matplotlib.plot as plot
liveimport.hidden_cell_magic(enabled=True)

which is followed by cell

#_%%liveimport --clear
import symcode
from printmath import print_math, print_equations as print_eq
from simulator import *

Additional Information

You can use LiveImport outside of notebooks, but in that case, you must call sync() to check for out of date imports. You can also disable automatic syncing in a notebook by calling auto_sync(enabled=False) and rely on explicit syncing instead.

Documentation

For more details including an API reference see liveimport.readthedocs.io.

Dependencies

LiveImport requires Python 3.10 or greater and IPython 7.1.1 or greater.

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

liveimport-0.9.1.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

liveimport-0.9.1-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file liveimport-0.9.1.tar.gz.

File metadata

  • Download URL: liveimport-0.9.1.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for liveimport-0.9.1.tar.gz
Algorithm Hash digest
SHA256 0eb9c7c5aea1272e6d04400e861f166f1d8dac6ba571ad16ca9bd3bc311de94e
MD5 89dec921736ded3518423df379b2b523
BLAKE2b-256 1056f21c837125d9f4d04ef5f663c90fcdecdd801c8c2fad8cc59f53117fb575

See more details on using hashes here.

File details

Details for the file liveimport-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: liveimport-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for liveimport-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5ab35bc80bd3a3931e444612ab3304b1f0d5d012e9dd05dc3784ea0a57b5fc6d
MD5 a22161782c3d0eed62f55193a1c52af1
BLAKE2b-256 885262548f7997735b0875647cda9935ba3f36674c68cf95d257f8c303589523

See more details on using hashes here.

Supported by

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