Lazy imports for Python
Lazy imports for Python
Posted Sep 14, 2022 0:02 UTC (Wed) by xnox (guest, #63320)In reply to: Lazy imports for Python by willy
Parent article: Lazy imports for Python
In python, import foo not only adds foo module to global namespace and allows accessing variables, functions, and classes that foo declares but it also typically executes any arbitrary code inside __init__ of said module which can have global side effects. For example establish network connections, migrating settings, writing out files or even simply setting process global environment variable.
import setup
import daemon
daemon.start()
Can fail, if setup has a side effect of exporting environment variables which start of Daemon expected to be set.
Same issues as shell source command, which also is hard to make "lazy".
