Skip to main content

More Imports! - Delayed importing

A few methods to make late importing cleaner

Branch Status
master Build Status
dev Build Status

Problem

Splitting code into modules is nice, but it can result in cyclic dependencies.

foos.py

from bars import bar

def foo():
    bar()

bars.py

from foos import foo

def bar():
    foo()

We are not concerned with the infinite recursion; this is only for demonstrating cyclic dependencies.

More Imports!

Solution #1: Use expect/export pattern

All your cyclic dependencies are covered with this one pattern: Break cycles by expecting a name in the first module, and let the second module export to the first when the value is available

foos.py

from mo_imports import expect

bar = expect("bar")

def foo():
    bar()

bars.py

from mo_imports import export
from foos import foo

def bar():
    foo()

export("bars", bar)

Benefits

  • every expect is verified to match with an export (and visa-versa)
  • using an expected variable before export raises an error
  • code is run only once, at module load time, not later
  • methods do not run import code
  • all "imports" are at the top of the file

Solution #2: Use delay_import

Provide a proxy which is responsible for import upon first use of the module variable.

foos.py

from mo_imports import delay_import

bar = delay_import("bars.bar")

def foo():
    bar()

bars.py

from foos import foo

def bar():
    foo()

Benefits

  • cleaner code
  • costly imports are delayed until first use

WARNING

Requires any of __call__, __getitem__, __getattr__ to be called to trigger the import. This means sentinals, placeholders, and default values can NOT be imported using delay_import()

Other solutions

If you do not use mo-imports your import cycles can be broken using one of the following common patterns:

Bad Solution: Keep in single file

You can declare yet-another-module that holds the cycles

foosbars.py

    def foo():
        bar()

    def bar():
        foo()

but this breaks the code modularity

Bad Solution: Use end-of-file imports

During import, setup of the first module is paused while it imports a second. A bottom-of-file import will ensure the first module is mostly setup to be used by the second.

foos.py

def foo():
    bar()

from bars import bar

bars.py

def bar():
    foo()

from foos import foo

Linters do not like this pattern: You may miss imports, since these are hiding at the bottom.

Bad Solution: Inline import

Import the name only when it is needed

foos.py

def foo():
    from bars import bar
    bar()

bars.py

def bar():
    from foos import foo
    foo()

This is fine for rarely run code, but there is an undesirable overhead because import is checked everytime the method is run. You may miss imports because they are hiding inline rather than at the top of the file.

Bad Solution: Use the _late_import() pattern

When other bad solutions do not work work, then importing late is the remaining option

foos.py

from bars import bar

def foo():
    bar()

bars.py

foo = None

def _late_import():
    global foo
    from foos import foo
    _ = foo

def bar():
    if not foo:
        _late_import()
    foo()

Placeholders variables are added, which linters complain about type. There is the added _late_import() method. You risk it is not run everywhere as needed. This has less overhead than an inline import, but there is still a check.

More on importing

Importing a complex modular library is still hard; the complexity comes from the the order other modules declare their imports; you have no control over which of your modules will be imported first. For example, one module may

from my_lib.bars import bar
from my_lib.foos import foo

another module may choose the opposite order

from my_lib.foos import foo
from my_lib.bars import bar

Ordering imports

With cyclic dependencies, ordering the imports can get tricky. Here are some rules

  • choose your principle modules and the order you want them imported.
  • your remaining modules are assumed to be imported in alphabetical order (as most linters prefer)
  • use top level __init__.py to control the order of imports
  • encourage third party modules to use this top level module. for example
    from my_lib import foo, bar
    
  • finally, use mo_imports to break cycles

Download files

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

Source Distribution

mo_imports-7.686.26234.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

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

mo_imports-7.686.26234-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file mo_imports-7.686.26234.tar.gz.

File metadata

  • Download URL: mo_imports-7.686.26234.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for mo_imports-7.686.26234.tar.gz
Algorithm Hash digest
SHA256 a82ccbf5d8406eec79516d48995ff488aa6a096ac9f2bd98fb7fe1ca7e3ad378
MD5 7b8ae46b72383298a69e42048c6c86d1
BLAKE2b-256 eb9b00a71083b1e2269d1481d2cc0f9c50a8a8b55c1213ec67f702a9dea3013d

See more details on using hashes here.

File details

Details for the file mo_imports-7.686.26234-py3-none-any.whl.

File metadata

File hashes

Hashes for mo_imports-7.686.26234-py3-none-any.whl
Algorithm Hash digest
SHA256 2ab89f2c384e35d2d7140fda012c85a45f0512e0ffe8ad77d4e113f4ba192f73
MD5 544de381a4766415729ed3e1701b4df4
BLAKE2b-256 47ed88feee1b8e8590e16d363554b7b91651f22bebcef98bd3695cf934d3e48f

See more details on using hashes here.

Release history Release notifications | RSS feed

7.687.26234

2 files

This release

7.686.26234 This release

2 files

7.685.25166

2 files

7.678.25061

2 files

7.677.25059

2 files

7.672.25036

2 files

7.671.25036

2 files

7.584.24095

2 files

7.546.24057

2 files

7.541.24038

2 files

7.531.24035

2 files

7.530.24035

2 files

7.527.24034

1 file

7.507.24028

1 file

7.502.24024

1 file

7.491.24021

1 file

7.490.24021

1 file

7.476.24007

1 file

7.449.23304

1 file

7.440.23265

1 file

7.430.23234

1 file

7.428.23214

1 file

7.416.23168

1 file

7.408.23161

1 file

7.401.23144

1 file

7.365.23080

1 file

7.341.23006

1 file

7.340.23006

1 file

7.339.23006

1 file

7.338.23006

1 file

7.298.22349

1 file

7.265.22338

1 file

7.251.22317

1 file

7.230.22310

1 file

7.187.22201

1 file

7.169.22121

1 file

7.168.22121

1 file

7.163.22119

1 file

7.158.22119

1 file

7.147.22086

1 file

7.109.22021

1 file

7.3.21313

1 file

6.2.21303

1 file

5.520.24032

1 file

5.519.24031

1 file

5.509.24030

1 file

5.17.21182

1 file

5.12.21182

1 file

3.149.20327

1 file

3.135.20303

1 file

3.108.20292

1 file

3.93.20259

1 file

3.87.20218

1 file

3.86.20207

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page