-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Display the invoked executable name when displaying help #91818
Labels
Comments
This could be done something like this: diff --git a/Lib/json/tool.py b/Lib/json/tool.py
index 0490b8c0be..e0e196c722 100644
--- a/Lib/json/tool.py
+++ b/Lib/json/tool.py
@@ -16,8 +16,11 @@
from pathlib import Path
+_python_exe = sys.executable
+
+
def main():
- prog = 'python -m json.tool'
+ prog = f'{_python_exe} -m json.tool'
description = ('A simple command line interface for json module '
'to validate and pretty-print JSON objects.')
parser = argparse.ArgumentParser(prog=prog, description=description) $ ./python.exe -m json.tool -h
usage: /Users/hugo/github/cpython/python.exe -m json.tool [-h] [--sort-keys] [--no-ensure-ascii] [--json-lines] [--indent INDENT | --tab | --no-indent | --compact]
[infile] [outfile]
... Or (less helpfully?) without the full path: diff --git a/Lib/json/tool.py b/Lib/json/tool.py
index 0490b8c0be..43754cdd59 100644
--- a/Lib/json/tool.py
+++ b/Lib/json/tool.py
@@ -12,12 +12,16 @@
"""
import argparse
import json
+import os
import sys
from pathlib import Path
+_python_exe = os.path.basename(sys.executable)
+
+
def main():
- prog = 'python -m json.tool'
+ prog = f'{_python_exe} -m json.tool'
description = ('A simple command line interface for json module '
'to validate and pretty-print JSON objects.')
parser = argparse.ArgumentParser(prog=prog, description=description) $ ./python.exe -m json.tool -h
usage: python.exe -m json.tool [-h] [--sort-keys] [--no-ensure-ascii] [--json-lines] [--indent INDENT | --tab | --no-indent | --compact] [infile] [outfile]
... pip does something along these lines: |
I did a little more than that with #91819 , partially to guarantee that something comes out when sys.executable might fail, since it can return |
The general issue is a duplicate of #66436. Leaving this issue open as |
serhiy-storchaka
added a commit
to serhiy-storchaka/cpython
that referenced
this issue
Oct 1, 2024
As argparse now detects by default when the code was run as a module. This leads to using the actual executable name instead of simply "python" to display in the usage message ("usage: python -m ...").
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Feature or enhancement
Display the invoked executable name such as python3 or python.exe so users can copy and paste without having to translate, for tools such as ast and json.tool.
Pitch
The example below shows a before usage of json.tool and an after below.
becomes
This is more friendly to users on windows because of the
.exe
extension and to *nix users who's OS might install python 3 at python3 and have an old python2 version somewhere else. Or as we move into the future and some OS drop python2 but never rectify python point to python3.Linked PRs
The text was updated successfully, but these errors were encountered: