Skip to content

Accessing one lazily imported submodule (lazy import pkg.a) also imports a later imported pkg.b #158140

Description

@msullivan

Bug report

Bug description:

If we do a lazy import of two submodules in the same package tree (like pkg.a and pkg.b), then accessing pkg.a will cause pkg.b to get imported first. I think this is because pkg gets bound specifically to a lazy_import object for pkg.b. (In fact, just accessing pkg also imports pkg.b).

main.py:

lazy import pkg.a
lazy import pkg.b

print(globals()['pkg'])  # <lazy_import 'pkg.b'>

pkg.a  # triggers import of pkg (of course), pkg.b (sketchy!!), pkg.a (of course)
pkg/
├── __init__.py   # print("pkg")
├── a.py          # print("pkg.a")
└── b.py          # print("pkg.b")

This prints:

<lazy_import 'pkg.b'>
pkg
pkg.b
pkg.a

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions