You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
argparse support for "python -m module" in help #66436
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2014-08-20.19:59:11.381>labels= ['type-feature', 'library']
title='argparse support for "python -m module" in help'updated_at=<Date2021-08-08.22:41:00.591>user='https://github.com/tebeka'
I suspect resolving this will actually need special casing on the argparse side - __main__.__spec__ will have the original details for __main__ when executed via -m of zipfile/directory execution.
Things to check for:
"__main__.__spec__ is not None" indicates a "-m" invocation
__main__.__spec__.name will give you the module name that was executed
if it is exactly "__main__" then this is likely zipfile or directory execution, and sys.argv[0] will still give the correct name
if it ends with a ".__main__", it is likely package execution, and the last segment should be dropped
otherwise, it is normal module execution
In the latter two cases, sys.executable can be used to figure out what to put before the "-m" (we don't expose the raw C level argv to Python, although there is an open RFE to do so somewhere on the tracker).
tebeka commented on Aug 22, 2014
tebekamannequin
MannequinAuthor
More actions
For zip file the help should probably be:
usage: python file.zip
tebeka commented on Aug 24, 2014
tebekamannequin
MannequinAuthor
More actions
New patch with handling of zip files.
paulj3 commented on Aug 24, 2014
paulj3mannequin
Mannequin
More actions
The patch tests assume the test is being run in a particular way:
python -m unittest ...
But I often use
python3 -m unittest ...
or
python3 test_argparse.py
both of which fail these tests.
Testing this change might be more difficult than implementing it.
paulj3 commented on Aug 24, 2014
paulj3mannequin
Mannequin
More actions
What if the package is run without -m?
tebeka commented on Aug 25, 2014
tebekamannequin
MannequinAuthor
More actions
Made the test more robust by using sys.executable in the comparison.
tebeka commented on Aug 25, 2014
tebekamannequin
MannequinAuthor
More actions
How can you run a package without -m?
paulj3 commented on Aug 25, 2014
paulj3mannequin
Mannequin
More actions
Package might be the wrong term. How about a directory with a __main__.py file, and no local imports (relative or not)? In my tests it runs with and without the -m.
In the first case, "subdir" will be added to sys.path, and then python will execute the equivalent of "python3 -m __main__"
In the second case, the current directory will be added to sys.path, and python will execute the equivalent of "python3 -m subdir.__main__"
The first case is the directory execution support that was added way back in Python 2.6.
The second case is a combination of the package execution support added in Python 2.7/3.1 and the implicit namespace packages support that was added in Python 3.3.
Interesting find - the possibility of the latter situation hadn't occurred to me before :)
tebeka commented on Aug 26, 2014
tebekamannequin
MannequinAuthor
More actions
Support for directory invocation as well.
tebeka commented on Sep 8, 2014
tebekamannequin
MannequinAuthor
More actions
Anything else I need to solve to get this patch accepted?
It looks to me that testing __main__.__spec__.name for '__main__' is enough. Additional checks are redundant, and checking that sys.argv[0] is equal to __main__.__spec__.origin with the trailing __main__.py removed is hard due to difference in normalization on Unix and Windows.
Activity
tebeka commentedon Aug 20, 2014
"python -m module -h" starts with
usage: __main__.py
It should be
usage: python -m module
ncoghlan commentedon Aug 21, 2014
I suspect resolving this will actually need special casing on the argparse side - __main__.__spec__ will have the original details for __main__ when executed via -m of zipfile/directory execution.
Things to check for:
In the latter two cases, sys.executable can be used to figure out what to put before the "-m" (we don't expose the raw C level argv to Python, although there is an open RFE to do so somewhere on the tracker).
tebeka commentedon Aug 22, 2014
For zip file the help should probably be:
usage: python file.zip
tebeka commentedon Aug 24, 2014
New patch with handling of zip files.
paulj3 commentedon Aug 24, 2014
The patch tests assume the test is being run in a particular way:
But I often use
or
both of which fail these tests.
Testing this change might be more difficult than implementing it.
paulj3 commentedon Aug 24, 2014
What if the package is run without
-m?tebeka commentedon Aug 25, 2014
Made the test more robust by using sys.executable in the comparison.
tebeka commentedon Aug 25, 2014
How can you run a package without -m?
paulj3 commentedon Aug 25, 2014
Package might be the wrong term. How about a directory with a
__main__.pyfile, and no local imports (relative or not)? In my tests it runs with and without the -m.ncoghlan commentedon Aug 26, 2014
If you have:
<curdir>
/subdir
__main__.py
Then in 3.3+, both of the following will work:
They do slightly different things, though.
In the first case, "subdir" will be added to sys.path, and then python will execute the equivalent of "python3 -m __main__"
In the second case, the current directory will be added to sys.path, and python will execute the equivalent of "python3 -m subdir.__main__"
The first case is the directory execution support that was added way back in Python 2.6.
The second case is a combination of the package execution support added in Python 2.7/3.1 and the implicit namespace packages support that was added in Python 3.3.
Interesting find - the possibility of the latter situation hadn't occurred to me before :)
tebeka commentedon Aug 26, 2014
Support for directory invocation as well.
tebeka commentedon Sep 8, 2014
Anything else I need to solve to get this patch accepted?
paulj3 commentedon Sep 8, 2014
When I apply
prog3.diffto my 3.5.0dev, and runI get 2 failures, both over 'usage: python3 -m [unit]test'.
It also fails with
I suspect it would also fail if I ran the tests with
nosetests(though I'm not setup to use that in the development version).12 remaining items
pythongh-66436: Improved prog default value for argparse.ArgumentParser
serhiy-storchaka commentedon Sep 30, 2024
#124799 mostly implements @ncoghlan's algorithm from #66436 (comment) and adds tests and documentation.
@ncoghlan, I would appreciate your review of the documentation, as it is difficult to me. Feel free to rewrite it completely.
pythongh-66436: Improved prog default value for argparse.ArgumentParser
serhiy-storchaka commentedon Oct 1, 2024
It looks to me that testing
__main__.__spec__.namefor'__main__'is enough. Additional checks are redundant, and checking thatsys.argv[0]is equal to__main__.__spec__.originwith the trailing__main__.pyremoved is hard due to difference in normalization on Unix and Windows.gh-66436: Improved prog default value for argparse.ArgumentParser (GH…
fix: handle argparse changes in Python 3.14
Handle argparse changes in Python 3.14
test: Fix argument parser tests on Python 3.14
test: Fix argument parser tests on Python 3.14
test: Fix argument parser tests on Python 3.14