[Tutor] Some questions about importlib

Peter Otten __peter__ at web.de
Sun Oct 21 06:22:43 EDT 2018

Quentin Agren wrote:

> Hi,
> My name Quentin, and this is my first post to this list so please redirect
> me if this is not the proper audience.
> I have been studying the 'importlib' standard library package this past
> week, and although I find it very readable I am puzzled by some questions
> that I'd like to share.
> - [Chicken and egg] Does the 'import' statement in Python make use of
> 'importlib'? If so, how are the imports in 'importlib' itself carried out?
> (for example in __init__.py)

Google found


> - Why does 'importlib' use 'nt' or 'psoix' (see
> '_bootstrap_external._setup()') rather than the portable 'os', and
> reimplement some of the latter's functionality, for example '_path_join',
> or '_path_split'. Another example is in 'SourceFileLoader.set_data()'
> where the logic of 'os.mkdirs' is reproduced to create all necessary
> intermediary directories in a file path.
> - Similarly, would not using the 'struct' module simplify the
> packing/unpacking of bytecode. For example by defining BYTECODE_HEADER_FMT
> = '4sII' (Python 3.6)

If the bootstrap process still works as Brett Cannon described it back in 
2012 struct and os cannot be used because they aren't built-in modules.

>>> import sys
>>> "posix" in sys.builtin_module_names
>>> "os" in sys.builtin_module_names
>>> "struct" in sys.builtin_module_names

More information about the Tutor mailing list