STYLE: Standardize Python shebangs on env python3 - #6844
Merged
hjmjohnson merged 4 commits intoSep 5, 2026
Merged
Conversation
PEP 394 does not guarantee a "python" command, and several distributions ship only "python3". Every one of these scripts already parses and runs under Python 3. Backport of InsightSoftwareConsortium#6842 on main.
A hardcoded interpreter path misses virtual environments and conda prefixes, and /usr/bin/python is Python 3 or absent on current systems. Backport of InsightSoftwareConsortium#6842 on main.
"#!python" names no interpreter path, so direct execution fails with ENOENT even though the file is marked executable. main carries a resolvable shebang here already. Backport of InsightSoftwareConsortium#6842 on main.
"/bin/env" does not exist on most systems; env lives in /usr/bin. main already uses the portable form here. Backport of InsightSoftwareConsortium#6842 on main.
hjmjohnson
marked this pull request as ready for review
September 5, 2026 16:01
Contributor
|
dzenanz
approved these changes
Sep 5, 2026
hjmjohnson
merged commit Sep 5, 2026
026ed21
into
InsightSoftwareConsortium:release-5.4
14 of 17 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport of #6842. Standardizes Python shebangs on
#!/usr/bin/env python3: 19 fromenv python, 2 from/usr/bin/python, one bare#!python, and one#!/bin/env python. One commit per shebang class.Independent of the removal backport — no shared files, and the two merge cleanly in either order (verified both directions).
Why python3 rather than python
PEP 394 does not guarantee that a
pythoncommand exists at all. Debian and Ubuntu ship none by default, and RHEL 8+ places it behindalternatives, so#!/usr/bin/env pythonis a coin flip on a current machine whilepython3resolves wherever Python 3 is installed.A hardcoded
#!/usr/bin/pythonis worse on both counts: it bypasses virtual environments and conda prefixes entirely, and on current systems that path is either Python 3 or absent — so the shebang no longer means what it said when it was written.Windows is not a counterargument. The py launcher (PEP 397) resolves
#!/usr/bin/env python3to the latest installed Python 3, sopython3is at least as portable there aspython.Verification that every touched script is Python 3
All 23 files:
ast.parse, 0 syntax errors) — also covered by thecheck-asthook;.iteritems(),.itervalues(),.iterkeys(),xrange(),raw_input(),basestring,urllib2,ConfigParser,.has_key()— no matches.The only two scripts in the tree that are Python 2 (
StripIncludes.pyuses.iteritems(),ParallelStripIncludes.pyusesxrange()) are deliberately excluded here; they are removed in the companion backport rather than given apython3shebang that would convert a silent failure into a runtime crash.Better long-term alternatives to shebang maintenance
Hand-maintained shebangs drift, which is how
#!pythonand#!/bin/env pythonsurvived onrelease-5.4for years. Options that remove the class of problem rather than fixing instances:python3 script.pyfrom documentation or CI. A script that is never executed directly does not need a shebang or an executable bit, and dropping both makes the intent explicit.[project.scripts]entry inpyproject.tomlgenerates a correct launcher per platform, including Windows.exeshims, and removes the question entirely.check-shebang-scripts-are-executable(already enabled here) catches the mismatch between shebang and mode but not an unresolvable interpreter. Ruff'sEXErules do check the interpreter itself —EXE003flags a shebang that does not namepython, andEXE001/EXE002cover the mode pairing on POSIX and Windows.Ruff is not currently a dependency on either branch — the Python hooks are
blackandpyupgrade— so anEXEgate would mean adding a hook repo, not toggling a setting. That is a separate change and is not proposed here.The bare
#!pythonhere is the finding Greptile raised on #6840;mainalready carries a resolvable shebang for that file.