<div dir="ltr">I should mention that I have a prototype design for improving importlib's lazy loading to be easier to turn on and use. See <a href="https://notebooks.azure.com/Brett/libraries/di2Btqj7zSI/html/Lazy%20importing.ipynb">https://notebooks.azure.com/Brett/libraries/di2Btqj7zSI/html/Lazy%20importing.ipynb</a> for my current notes. Part of it includes an explicit lazy_import() function which would negate needing to hide imports in functions to delay their importation.<br><div><div><br><div class="gmail_quote"><div dir="ltr">On Wed, 6 Sep 2017 at 20:50 INADA Naoki <<a href="mailto:songofacandy@gmail.com">songofacandy@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">> I’m not sure however whether burying imports inside functions (as a kind of poor man’s lazy import) is ultimately going to be satisfying.  First, it’s not natural, it generally violates coding standards (e.g. PEP 8), and can make linters complain.<br>
<br>
Of course,  I tried to move imports only when (1) it's only used one<br>
or two of many functions in the module,<br>
(2) it's relatively heavy, (3) it's rerely imported from other modules.<br>
<br>
<br>
>   Second, I think you’ll end up chasing imports all over the stdlib and third party modules in any sufficiently complicated application.<br>
<br>
Agree.  I won't use much time to optimization by moving import from<br>
top to inner function in stdlib.<br>
<br>
I think my import-profiler patch can be polished and committed in Python to help<br>
library maintainers to know import time easily.  (Maybe, `python -X<br>
import-profile`)<br>
<br>
<br>
> Third, I’m not sure that the gains you’ll get won’t just be overwhelmed by lots of other things going on, such as pkg_resources entry point processing, pth file processing, site.py effects, command line processing libraries such as click, and implicitly added distribution exception hooks (e.g. Ubuntu’s apport).<br>
<br>
Yes.  I noticed some of them while profiling imports.<br>
For example, old-style namespace package imports types module for types.Module.<br>
Types module imports functools, and functools imports collections.<br>
So some efforts in CPython (Avoid importing collections and functools<br>
from site) is not<br>
worth enough when there are at least one old-style namespace package<br>
is installed.<br>
<br>
<br>
><br>
> Many of these can’t be blamed on Python itself, but all can contribute significantly to Python’s apparent start up time.  It’s definitely worth investigating the details of Python import, and a few of us at the core sprint have looked at those numbers and thrown around ideas for improvement, but we’ll need to look at the effects up and down the stack to improve the start up performance for the average Python application.<br>
><br>
<br>
Yes. I totally agree with you.  That's why I use import-profile.patch<br>
for some 3rd party libraries.<br>
<br>
Currently, I have these ideas to optimize application startup time.<br>
<br>
* Faster, or lazily compiling regular expression. (pkg_resources<br>
imports pyparsing, which has lot regex)<br>
* More usable lazy import. (which can be solved "PEP 549: Instance<br>
Properties (aka: module properties)")<br>
* Optimize enum creation.<br>
* Faster namedtuple (There is pull request already)<br>
* Faster ABC<br>
* Breaking large import tree in stdlib.  (PEP 549 may help this too)<br>
<br>
Regards,<br>
<br>
INADA Naoki  <<a href="mailto:songofacandy@gmail.com" target="_blank">songofacandy@gmail.com</a>><br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/brett%40python.org" rel="noreferrer" target="_blank">https://mail.python.org/mailman/options/python-dev/brett%40python.org</a><br>
</blockquote></div></div></div></div>