# Python modules and imports

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-modules-imports-full-en
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.

(Test: 3 tests were run, TE-47-A, TE-47-B and TE-47-C. The expected non-zero exit is TE-47-C (exit status 1). Every row has a recorded log in the evidence archive. The evidence table lists the exit status and log hash of every row, and no Exit status line on this page is for the A row.)

### 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`):

```bash
printf 'print("loaded")\n' > m.py; printf 'import m\nimport m\nprint("done")\n' > use.py; printf 'import nosuch\n' > bad.py
```

Command:

```bash
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=1
```

### TE-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`):

```bash
printf 'x = 5\n' > m.py; printf 'import m\nprint(m.x)\nfrom m import x\nprint(x)\n' > use.py
```

Command:

```bash
python3 use.py
```

Recorded output:

```
5
5
```

Exit 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`):

```bash
printf 'from math import nosuch\n' > s.py
```

Command:

```bash
python3 s.py
```

Recorded 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

- https://docs.python.org/3/tutorial/modules.html

## 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.

## Related guides
- [Python dates: parse and compare](https://kairos-2z6s.onrender.com/res/python-dates-full-en)
- [Python: count with a dict or Counter, and meet sets and tuples](https://kairos-2z6s.onrender.com/res/python-counting-sets-tuples-full-en)
- [Python lru_cache: remember function results](https://kairos-2z6s.onrender.com/res/python-lru-cache-full-en)
- [Python os.environ: read environment variables](https://kairos-2z6s.onrender.com/res/python-os-environ-full-en)
- [Python max and min with key: pick by a rule](https://kairos-2z6s.onrender.com/res/python-max-min-key-full-en)
- [Python pathlib: join paths, read a file and list a folder](https://kairos-2z6s.onrender.com/res/python-pathlib-full-en)
