ndifftest.py fails to import on Python 3.12+ (imp module removed)
ndifftest.py fails to import on Python 3.12+ (imp module removed)
Component: Ndiff (test harness only)
Affects: Python 3.12 and newer. Not macOS-specific.
Tested at: master b97fabd935362f85ad0575632e6d887f4d778b59
Summary
ndiff/ndifftest.py:15 does import imp. The imp module was deprecated in
Python 3.4 and removed in 3.12, so the Ndiff test suite dies at import on any
interpreter from 3.12 onward. The tests themselves are fine — this is purely the
harness's module-loading shim.
Reproduction
cd ndiff && ./ndifftest.py| Interpreter | Result |
|---|---|
| 3.9.6 | exit 0 — Ran 57 tests ... OK |
| 3.11.15 | exit 0 — Ran 57 tests ... OK |
| 3.12.11 | exit 1 — ModuleNotFoundError: No module named 'imp' |
| 3.14.7 | exit 1 — ModuleNotFoundError: No module named 'imp' |
Through the build system, with Ndiff configured in:
$ make check-ndiff
make: *** [check-ndiff] Error 1Fix
imp.load_source() has a documented replacement:
import importlib.util
_spec = importlib.util.spec_from_file_location("ndiff", "ndiff.py")
ndiff = importlib.util.module_from_spec(_spec)
sys.modules["ndiff"] = ndiff # preserves imp.load_source's side effect
_spec.loader.exec_module(ndiff)After the change, all four interpreters above report Ran 57 tests ... OK, and
make check-ndiff exits 0.
Scope, stated honestly
- This restores the 57 tests that already passed on ≤3.11. It adds no coverage.
ndiff.pyitself is unaffected — it imports no removed modules and runs correctly on 3.12+.- This does not break GitHub Actions CI:
.github/workflows/build.ymldoes not runmake checkon any of its legs. - It is masked whenever
configure's chosen python3 lackssetuptools/build, because Ndiff is then skipped entirely (WARNING: Not building Ndiff because Python with setuptools was not found) andcheck-ndiffis omitted from the generatedcheck:target.
Prior art
This looks like the remaining half of #2649, which migrated Ndiff to setuptools and was closed as completed. The CHANGELOG entry for it is explicitly packaging-only:
o [Zenmap][Ndiff][GH#2649] Zenmap and Ndiff now use setuptools, not distutils for packaging.
The Python 3 port of Ndiff itself (12d41ec2cd, "Closes #1807") also walked past
this line and left it.
Two distributions already carry exactly this patch downstream:
- GNU Guix — "gnu: nmap: Fix tests under [email protected]." (2026-02-07)
substitutes
import impforimport importlib.utiland swaps inspec_from_file_location/module_from_spec/exec_module. - BLFS —
sed -e '/import imp/d' -e 's/^ndiff = .*$/import ndiff/' -i ndiff/ndifftest.py
Two unrelated packagers patching the same two lines suggests it is worth fixing upstream rather than downstream repeatedly.
Searched issues and PRs (open, closed and merged) for ndifftest, imp module,
python 3.12, importlib, load_source, ModuleNotFoundError imp; and the
nmap-dev archive. No existing report found.
Tested on macOS 26 (arm64) only.
Source: nmap/nmap