Title: Theory / Important / CS / Programming / Python / Manual{ / 5 import
Tags: FromInstruction computer_to_instruction hasNotNewEffectReturnsMeaning hasSeenConjDef
On every note of this deck, unless a note says otherwise:
Page: https://docs.python.org/3/reference/import.html
Page: https://docs.python.org/3/reference/import.html
Page: https://docs.python.org/3/reference/import.html

Name: import
Definition: searches for the named module
Chapter: 5
Context: python
Definition2: create a module object with the previous result
Conjdef: in this order
Definition3: binds the previous results to a name in the local scope
Definition4: raise ModuleNotFoundError if it fails

Instruction: importlib
Language: Python
Chapter: 5
Returns: a rich API for interacting with the import system

Name: package
Definition: organized hierarchically, separated by dot
Chapter: 5
Section: 2
Context: python
Definition2: module containing a __path__ attribute
Conjdef: All of
Intuition: the directories on a file system
Typ: modules

Instruction: import package_1. ... .package_n
Effect: For i from 1 to n:
Consider the file package_1. ... .package_i
if it is already considered, do nothing
otherwise, import it
Language: Python
Chapter: 5
Section: 2.1
Extra: importation of modules, whose parents are already imported, is seen in another card

Instruction: import package_1. ... .package_n
Effect: execute its __init__.py file, bind its value to the namespace of this package
Language: Python
Chapter: 5
Section: 3
Variables: package_n is a regular package
Assuming: each ancestors are already imported

Instruction: import package_1. ... .package_n
Effect: crate a namespace package, and use its __path__ value to find the subpackages
Language: Python
Chapter: 5
Section: 3
Variables: package_n is a namespace package
Assuming: each ancestors are already imported

Question: Where to search for a module
Answer: module cache sys.modules of modules already imported
Chapter: 5
Section: 3
Context: python
import package_1. ... .package_n . module
Answer2: test each finder for this module, give its module spac to the related loader
Assuming: Default search is used.

Instruction: sys.modules[module]=None
Effect: Next import of module will raise a ModuleNotFoundError
Language: Python
Context: import package_1. ... .package_n . module
Chapter: 5
Section: 3.1
Variables: module a loaded module

Instruction: del sys.modules[module]
Effect: Next import of module will create a new module, instead of using the object from the previous import
Language: Python
Context: import package_1. ... .package_n . module
Chapter: 5
Section: 3.1
Variables: module a loaded module

Instruction: importlib.reload(module)
Effect: reexecute the module code, without changing anything else (for example, its namespace is the same dictionnary as the namespace before reloading)
Language: Python
Context: import package_1. ... .package_n . module
Chapter: 5
Section: 3.1
Variables: module a loaded module

Name: import hooks
Definition: meta hooks
Chapter: 5
Section: 3.3
Context: python
Definition2: import path hooks
Conjdef: Either

Name: meta hooks
Definition: registred through sys.meta_path
Chapter: 5
Section: 3.3
Context: python
Definition2: executed after sys.modules look-up, before any other loading action
Conjdef: a hook
Definition3: contains a find_spec(name,import path,[target module]) function, returning None or a spec object, with path beeing the parent's __path__ attribute

Name: Import path hooks
Definition: registred through sys.path_hook
Chapter: 5
Section: 3.3
Context: python
Definition2: called as part of sys.path (or package.__path__) processing,
Conjdef: a hook

Question: The default finders looks for
Answer: built-in modules
Chapter: 5
Section: 3.4
Context: python
Answer2: frozen modules
Answer3: import path

Name: loader
Definition: Create a module object
Chapter: 5
Section: 3.4
Context: python
Definition2: add the module to sys.modules (remove it in case of failure later)
Conjdef: in this order
Definition3: execute the module (according to its own rule, and not necessiraly standard python rule)
Definition4: return a module (not necessarily the one created above)
Definition5: If the module is a Python module (as opposed to a built-in module or a dynamically loaded extension), it should execute the module’s code in the module’s global name space (module.__dict__).
Definition6: should raise ImportError if module can not be executed

Instruction: .bar
Effect: the module bar which is the child of foo
Language: Python
Context: package foo
Chapter: 5
Section: 4.1

Question: How to change import system
Answer: replace sys.meta_path with custom meta path hook, to change the entire process
Chapter: 5
Section: 6
Answer2: replace __import__() to change direct import, but not the other API's that access import system

Question: __main__.__spec__ value when -m is used
Answer: the standard value
Chapter: 5
Section: 7
Variables: p a package
Context: python

Question: __main__.__spec__ value when it is executed while importing a directory, zypfile or sys.path entry
Answer: the standard value
Chapter: 5
Section: 7
Variables: p a package
Context: python

Question: __main__.__spec__ value when it is executed from interactive prompt
Answer: None
Chapter: 5
Section: 7
Variables: p a package
Context: python

Question: __main__.__spec__ value, when it is executed with -c switch
Answer: None
Chapter: 5
Section: 7
Variables: p a package
Context: python

Question: __main__.__spec__ value, when it is executed from stdin
Answer: None
Chapter: 5
Section: 7
Variables: p a package
Context: python

Question: __main__.__spec__ value, when it is executed directly from a source/bytecode file
Answer: None
Chapter: 5
Section: 7
Variables: p a package
Context: python