Python modules and imports
A module is a unit of code that the interpreter loads once per session; import binds the loaded module to a name.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-47-A: a module body runs once per interpreter, and a missing module raises ModuleNotFoundError
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'print("loaded")\n' > m.py; printf 'import m\nimport m\nprint("done")\n' > use.py; printf 'import nosuch\n' > bad.pyCommand:
python3 use.py; python3 bad.py 2>&1 | tail -1; python3 bad.py >/dev/null 2>&1; echo exit=$?Recorded output:
loaded
done
ModuleNotFoundError: No module named 'nosuch'
exit=1TE-47-B: import m gives access as m.x, and from m import x binds the name x directly
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'x = 5\n' > m.py; printf 'import m\nprint(m.x)\nfrom m import x\nprint(x)\n' > use.pyCommand:
python3 use.pyRecorded output:
5
5Exit status: 0. This status and the output match what was written before the run.
TE-47-C: expected non-zero exit: importing a name that the module does not have raises ImportError
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'from math import nosuch\n' > s.pyCommand:
python3 s.pyRecorded output: none, standard output was empty.
Standard error from the same run:
Traceback (most recent call last):
File "/tmp/b14-o540f89g/work/s.py", line 1, in <module>
from math import nosuch
ImportError: cannot import name 'nosuch' from 'math' (unknown location)Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.
TE-47-B: by the setup, m.py holds x = 5. import m made m.x read 5, and from m import x made x read 5. TE-47-C: from math import nosuch stopped with exit status 1. The last line of standard error is ImportError: cannot import name 'nosuch' from 'math' (unknown location).
Limits
Built-in modules exist; a second import reuses the loaded module; ModuleNotFoundError differs from ImportError.
Source
Evidence
TE-47-A: a module body runs once per interpreter, and a missing module raises ModuleNotFoundError
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-47-A | a module body runs once per interpreter, and a missing module raises ModuleNotFoundError | 0 | PASS | af1f34ab3d4d | | TE-47-B | import m gives access as m.x, and from m import x binds the name x directly | 0 | PASS | b84524d2a745 | | TE-47-C | expected non-zero exit: importing a name that the module does not have raises ImportError | 1 | PASS | 55c9fa7eed24 |
Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides