Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a loaded module and an initialized module?

In the reference manual it is stated that:

A complete Python program is executed in a minimally initialized environment: all built-in and standard modules are available, but none have been initialized, except for sys (various system services), builtins (built-in functions, exceptions and None) and __main__.

I am uncertain about what "initialized" is supposed to mean here. I always thought that a module was initialized if it was loaded and present in sys.modules:

This is a dictionary that maps module names to modules which have already been loaded.

Apparently, I was wrong because sys.modules contains many other modules:

python -c "import sys; print(sys.modules.keys() - {'sys', 'builtins', '__main__'})"
{'_stat', 'encodings.aliases', '_sitebuiltins', '_thread', 'io', '_weakrefset', 'genericpath', 'encodings.utf_8', 'codecs', 'os', '_weakref', '_codecs', '_frozen_importlib', '_io', '_frozen_importlib_external', 'os.path', '_warnings', '_bootlocale', '_signal', 'errno', '_imp', 'encodings.latin_1', 'sysconfig', 'marshal', 'encodings', 'usercustomize', 'site', 'posixpath', '_collections_abc', 'posix', '_sysconfigdata_m_linux_x86_64-linux-gnu', 'encodings.cp437', 'abc', 'zipimport', 'stat', '_locale'}

What is the difference between an initialized and a loaded module? I'm on Python 3.

like image 687
Bob Holver Avatar asked Dec 03 '25 08:12

Bob Holver


1 Answers

The language initialization has gotten a lot more complicated since that documentation was written. (It's been mostly unchanged since at least Python 1.4.) All those modules in sys.modules are fully loaded and initialized.

like image 135
user2357112 supports Monica Avatar answered Dec 05 '25 21:12

user2357112 supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!