<story><title>The return of lazy imports for Python</title><url>https://lwn.net/Articles/917280/</url></story><parent_chain></parent_chain><comment><author>kortex</author><text>I really hope a solution can be found and python can get true lazy imports (without doing the manual thing). I work in the ML&amp;#x2F;AI space and manual lazy imports are basically mandatory unless you want several seconds to see usage text from `footool --help`. Why &amp;quot;pay&amp;quot; for executing code you don&amp;#x27;t use?&lt;p&gt;Manual lazy import meaning:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    def uses_foo(x):
        import foo
        return foo.bar(x)
&lt;/code&gt;&lt;/pre&gt;
Manual lazy import sucks because:&lt;p&gt;- it&amp;#x27;s just ugly, I like imports all at the top so I can see all the deps&lt;p&gt;- bad for static analysis&lt;p&gt;- performance hit every time the function is called&lt;p&gt;Eschewing lazy imports has several problems:&lt;p&gt;- you always pay the execution cost, even if you don&amp;#x27;t use it&lt;p&gt;- also bad for static analysis and testing, since you have to eat the import time even if the code block you want to test doesn&amp;#x27;t execute the expensive path&lt;p&gt;- sometimes you &lt;i&gt;need&lt;/i&gt; lazy import to avoid circular import errors&lt;p&gt;It&amp;#x27;s too bad that the main impediment to this is existing code which relies on side effects. Import-time side effects are an absolute pain in the ass. Avoid it at all costs.</text></comment>
<story><title>The return of lazy imports for Python</title><url>https://lwn.net/Articles/917280/</url></story><parent_chain></parent_chain><comment><author>m000</author><text>Not a word though about the elephant in the room: circular imports.&lt;p&gt;It is absurdly easy in Python to end up with a circular import situtation, where no real circular dependency exists. E.g. you can&amp;#x27;t have A.a1 -&amp;gt; B.b1 and B.b2 -&amp;gt; A.a2. So, you are forced to layout your code in some quite awkward ways.</text></comment>