Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #197746 > unrolled thread

Lazy Imports -- I Like This Idea

Started byLawrence D’Oliveiro <ldo@nz.invalid>
First post2026-03-26 01:40 +0000
Last post2026-04-03 13:33 +0200
Articles 13 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Lazy Imports -- I Like This Idea Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-03-26 01:40 +0000
    Re: Lazy Imports -- I Like This Idea Piergiorgio Sartor <piergiorgio.sartor.this.should.not.be.used@nexgo.REMOVETHIS.de> - 2026-03-27 19:09 +0100
    Re: Lazy Imports -- I Like This Idea DFS <nospam@dfs.com> - 2026-03-28 00:59 -0400
      Re: Lazy Imports -- I Like This Idea Piergiorgio Sartor <piergiorgio.sartor.this.should.not.be.used@nexgo.REMOVETHIS.de> - 2026-03-28 11:40 +0100
      Re: Lazy Imports -- I Like This Idea Jason H <jason_hindle@yahoo.com> - 2026-04-14 22:16 +0000
    Re: Lazy Imports -- I Like This Idea Gilmeh Serda <gilmeh.serda@nothing.here.invalid> - 2026-03-29 13:49 +0000
      Re: Lazy Imports -- I Like This Idea Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-03-29 22:35 +0000
        Re: Lazy Imports -- I Like This Idea Paul Rubin <no.email@nospam.invalid> - 2026-03-29 19:03 -0700
          Re: Lazy Imports -- I Like This Idea Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-03-30 02:17 +0000
            Re: Lazy Imports -- I Like This Idea Paul Rubin <no.email@nospam.invalid> - 2026-03-30 11:21 -0700
        Re: Lazy Imports -- I Like This Idea Gilmeh Serda <gilmeh.serda@nothing.here.invalid> - 2026-04-03 11:12 +0000
          Re: Lazy Imports -- I Like This Idea Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-04-03 22:45 +0000
      Re: Lazy Imports -- I Like This Idea Piergiorgio Sartor <piergiorgio.sartor.this.should.not.be.used@nexgo.REMOVETHIS.de> - 2026-04-03 13:33 +0200

#197746 — Lazy Imports -- I Like This Idea

FromLawrence D’Oliveiro <ldo@nz.invalid>
Date2026-03-26 01:40 +0000
SubjectLazy Imports -- I Like This Idea
Message-ID<10q22pv$2f060$2@dont-email.me>
So Python 3.15 will introduce a new, “lazy” import mechanism
<https://peps.python.org/pep-0810/>.

So far I have done one script where I moved an import into the
function where it was used, instead of doing it globally; this reduced
the script startup time from around 1.5 seconds down to about a
quarter second.

“Lazy” imports would avoid the need for such workarounds, while
keeping all imports together so they can be found more easily.

[toc] | [next] | [standalone]


#197747

FromPiergiorgio Sartor <piergiorgio.sartor.this.should.not.be.used@nexgo.REMOVETHIS.de>
Date2026-03-27 19:09 +0100
Message-ID<doti9m-kab.ln1@lazy.lzy>
In reply to#197746
On 26/03/2026 02.40, Lawrence D’Oliveiro wrote:
> So Python 3.15 will introduce a new, “lazy” import mechanism
> <https://peps.python.org/pep-0810/>.
> 
> So far I have done one script where I moved an import into the
> function where it was used, instead of doing it globally; this reduced
> the script startup time from around 1.5 seconds down to about a
> quarter second.
> 
> “Lazy” imports would avoid the need for such workarounds, while
> keeping all imports together so they can be found more easily.

Interesting, I needed this as well.
Last time I checked, the suggestion
was rejected...

Good if they changed their mind.

bye,

-- 

piergiorgio

[toc] | [prev] | [next] | [standalone]


#197748

FromDFS <nospam@dfs.com>
Date2026-03-28 00:59 -0400
Message-ID<10q7n78$atfj$2@dont-email.me>
In reply to#197746
On 3/25/2026 9:40 PM, Lawrence D’Oliveiro wrote:

> So Python 3.15 will introduce a new, “lazy” import mechanism
> <https://peps.python.org/pep-0810/>.
> 
> So far I have done one script where I moved an import into the
> function where it was used, instead of doing it globally; this reduced
> the script startup time from around 1.5 seconds down to about a
> quarter second.
> 
> “Lazy” imports would avoid the need for such workarounds, while
> keeping all imports together so they can be found more easily.


Faster startup is nice, but lazy importing doesn't reduce total runtime 
of the program, right?

[toc] | [prev] | [next] | [standalone]


#197749

FromPiergiorgio Sartor <piergiorgio.sartor.this.should.not.be.used@nexgo.REMOVETHIS.de>
Date2026-03-28 11:40 +0100
Message-ID<fqnk9m-594.ln1@lazy.lzy>
In reply to#197748
On 28/03/2026 05.59, DFS wrote:
[...]
> Faster startup is nice, but lazy importing doesn't reduce total runtime 
> of the program, right?

Different objective, anyway.

It *might* reduce total runtime, it depends
on the code structure.

If the code is multi-threaded, likely having
the imports *per thread* is better than having
them globally.

YMMV

bye,

-- 

piergiorgio

[toc] | [prev] | [next] | [standalone]


#197764

FromJason H <jason_hindle@yahoo.com>
Date2026-04-14 22:16 +0000
Message-ID<10rmeco$ftk4$1@dont-email.me>
In reply to#197748
On 28/03/2026 04:59, DFS wrote:
>On 3/25/2026 9:40 PM, Lawrence D’Oliveiro wrote:
>
>> So Python 3.15 will introduce a new, “lazy” import mechanism
>> <https://peps.python.org/pep-0810/>.
>> 
>> So far I have done one script where I moved an import into the
>> function where it was used, instead of doing it globally; this reduced
>> the script startup time from around 1.5 seconds down to about a
>> quarter second.
>> 
>> “Lazy” imports would avoid the need for such workarounds, while
>> keeping all imports together so they can be found more easily.
>
>
>Faster startup is nice, but lazy importing doesn't reduce total runtime 
>of the program, right?
>

Perhaps. Perhaps not. If the conditions for the import to run at all are
 never met, total execution time can be faster. Also, there is a history of
 psychology and smoke and mirrors in the world of software. 


-- 
--
A PICKER OF UNCONSIDERED TRIFLES

[toc] | [prev] | [next] | [standalone]


#197750

FromGilmeh Serda <gilmeh.serda@nothing.here.invalid>
Date2026-03-29 13:49 +0000
Message-ID<p5ayR.164248$o4va.120026@fx17.ams4>
In reply to#197746
On Thu, 26 Mar 2026 01:40:15 -0000 (UTC), Lawrence D’Oliveiro wrote:

> “Lazy”

[PEP] TL;DR

From the initial idea I got that now we have to have the word lazy 
scattered all over the place for every module that needs to be loaded in 
such a manner:

lazy import blah
lazy import whatever
lazy from thisorthat import meh


Wouldn't it have been better if they used, e.g. "with" as a starter? Like 
so:

with lazy:
    import blah
    import whatever
    from thisorthat import meh

Then it would have been much smoother to handle.

My 2¢ worth...

-- 
Gilmeh

"Plaese porrf raed." -- Prof. Michael O'Longhlin, S.U.N.Y. Purchase

[toc] | [prev] | [next] | [standalone]


#197751

FromLawrence D’Oliveiro <ldo@nz.invalid>
Date2026-03-29 22:35 +0000
Message-ID<10qc9fp$1v9oe$1@dont-email.me>
In reply to#197750
On Sun, 29 Mar 2026 13:49:09 GMT, Gilmeh Serda wrote:

> From the initial idea I got that now we have to have the word lazy
> scattered all over the place for every module that needs to be
> loaded in such a manner:
>
> lazy import blah
> lazy import whatever
> lazy from thisorthat import meh
>
>
> Wouldn't it have been better if they used, e.g. "with" as a starter?
> Like so:
>
> with lazy:
>     import blah
>     import whatever
>     from thisorthat import meh
>
> Then it would have been much smoother to handle.

I don’t think it will be needed for a great many modules. In my case,
it was only ever an issue with matplotlib.

[toc] | [prev] | [next] | [standalone]


#197752

FromPaul Rubin <no.email@nospam.invalid>
Date2026-03-29 19:03 -0700
Message-ID<87qzp2axb3.fsf@nightsong.com>
In reply to#197751
Lawrence D’Oliveiro <ldo@nz.invalid> writes:
> I don’t think it will be needed for a great many modules. In my case,
> it was only ever an issue with matplotlib.

I defer importing bs4 sometimes, depending on what I'm doing.

What we really need is a way to dump out a loadable image after doing
all the imports, so you can reload your app with almost no import lag.
Lisps have supported that since our grandparents' era.  Emacs still has
something like it (they gave up on the classic unexec though), and
gforth has something similar too.

Maybe CRIU could be adapted for this purpose.

[toc] | [prev] | [next] | [standalone]


#197753

FromLawrence D’Oliveiro <ldo@nz.invalid>
Date2026-03-30 02:17 +0000
Message-ID<10qcmg5$238e1$1@dont-email.me>
In reply to#197752
On Sun, 29 Mar 2026 19:03:12 -0700, Paul Rubin wrote:

> Lawrence D’Oliveiro <ldo@nz.invalid> writes:
>>
>> I don’t think it will be needed for a great many modules. In my
>> case, it was only ever an issue with matplotlib.
>
> I defer importing bs4 sometimes, depending on what I'm doing.

That same script imports bs4, as it happens. It still manages an
overall startup time of just a quarter second.

[toc] | [prev] | [next] | [standalone]


#197754

FromPaul Rubin <no.email@nospam.invalid>
Date2026-03-30 11:21 -0700
Message-ID<87h5pxb2kc.fsf@nightsong.com>
In reply to#197753
Lawrence D’Oliveiro <ldo@nz.invalid> writes:
> That same script imports bs4, as it happens. It still manages an
> overall startup time of just a quarter second.

Importing bs4 can take a few hundred msec on the cheap VPS that I use it
on.  So deferring it speeds the program startup by that much.

[toc] | [prev] | [next] | [standalone]


#197757

FromGilmeh Serda <gilmeh.serda@nothing.here.invalid>
Date2026-04-03 11:12 +0000
Message-ID<GgNzR.62652$Z41.25427@fx01.ams4>
In reply to#197751
On Sun, 29 Mar 2026 22:35:37 -0000 (UTC), Lawrence D’Oliveiro wrote:

> I don’t think it will be needed for a great many modules.

One cannot say with certainty that this is the case. Let's revisit the 
issue when things are fully implemented and we will see how people will 
use it. Knowing "people" (a person can be intelligent, but people are 
generally morons) I don't think this is the end of the story.

-- 
Gilmeh

Join the Navy; sail to far-off exotic lands, meet exciting interesting 
people, and kill them.

[toc] | [prev] | [next] | [standalone]


#197759

FromLawrence D’Oliveiro <ldo@nz.invalid>
Date2026-04-03 22:45 +0000
Message-ID<10qpfv4$8qc0$2@dont-email.me>
In reply to#197757
On Fri, 03 Apr 2026 11:12:38 GMT, Gilmeh Serda wrote:

> On Sun, 29 Mar 2026 22:35:37 -0000 (UTC), Lawrence D’Oliveiro wrote:
> 
>> I don’t think it will be needed for a great many modules.
> 
> One cannot say with certainty that this is the case.

Just based on past experience.

[toc] | [prev] | [next] | [standalone]


#197758

FromPiergiorgio Sartor <piergiorgio.sartor.this.should.not.be.used@nexgo.REMOVETHIS.de>
Date2026-04-03 13:33 +0200
Message-ID<46l4am-j35.ln1@lazy.lzy>
In reply to#197750
On 29/03/2026 15.49, Gilmeh Serda wrote:
> On Thu, 26 Mar 2026 01:40:15 -0000 (UTC), Lawrence D’Oliveiro wrote:
> 
>> “Lazy”
> 
> [PEP] TL;DR
> 
>  From the initial idea I got that now we have to have the word lazy
> scattered all over the place for every module that needs to be loaded in
> such a manner:
> 
> lazy import blah
> lazy import whatever
> lazy from thisorthat import meh
> 
> 
> Wouldn't it have been better if they used, e.g. "with" as a starter? Like
> so:
> 
> with lazy:
>      import blah
>      import whatever
>      from thisorthat import meh
> 
> Then it would have been much smoother to handle.

Why?

It's one line more and large overhead for few imports.
And the IDE can expand automatically the "lazy" part.

Furthermore, it can always be added later, as alternative
to explicit "lazy" in front of the import.

So, I personally do not see this as needed feature now.

> My 2¢ worth...

Same...

bye,

-- 

piergiorgio

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web