GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — argparse support for "python -m module" in help · Issue #66436 · python/cpython

argparse support for "python -m module" in help #66436

New issue
Copy link
New issue
Copy link
Closed
Closed
argparse support for "python -m module" in help#66436
Copy link
Assignees
serhiy-storchaka
Labels
3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

tebeka
mannequin
opened on Aug 20, 2014
Issue body actions
BPO 22240
Nosy @tebeka, @ncoghlan, @jwilk, @zertrin, @SamuelMarks, @septatrix
Files
  • prog.diff: Patch
  • prog2.diff: Patch
  • prog3.diff: Patch
  • prog3.diff: Patch
  • prog-module-name-handler.patch: Patch for handling python -m syntax in argument parser ouptut
  • patch.diff
  • 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 = None
    closed_at = None
    created_at = <Date 2014-08-20.19:59:11.381>
    labels = ['type-feature', 'library']
    title = 'argparse support for "python -m module" in help'
    updated_at = <Date 2021-08-08.22:41:00.591>
    user = 'https://github.com/tebeka'

    bugs.python.org fields:

    activity = <Date 2021-08-08.22:41:00.591>
    actor = 'Nils Kattenbeck'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2014-08-20.19:59:11.381>
    creator = 'tebeka'
    dependencies = []
    files = ['36424', '36453', '36461', '36476', '48643', '50174']
    hgrepos = []
    issue_num = 22240
    keywords = ['patch']
    message_count = 23.0
    messages = ['225586', '225608', '225669', '225831', '225834', '225838', '225862', '225863', '225865', '225911', '225913', '226578', '226606', '226622', '226697', '226706', '226707', '353987', '353989', '397764', '398076', '398698', '399244']
    nosy_count = 9.0
    nosy_names = ['tebeka', 'ncoghlan', 'peter.otten', 'bethard', 'jwilk', 'paul.j3', 'zertrin', 'samuelmarks', 'Nils Kattenbeck']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue22240'
    versions = ['Python 3.5']

    Linked PRs

    • gh-66436: Improved prog default value for argparse.ArgumentParser #124799

    Activity

    Next
    1. tebeka commented on Aug 20, 2014

      @tebeka
      tebekamannequin
      on Aug 20, 2014
      MannequinAuthor
      More actions

      "python -m module -h" starts with
      usage: __main__.py

      It should be
      usage: python -m module

    2. tebeka
      added
      stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancementA feature request or enhancement
      on Aug 20, 2014
    3. ncoghlan commented on Aug 21, 2014

      @ncoghlan
      ncoghlan
      on Aug 21, 2014
      Contributor
      More actions

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

    4. tebeka commented on Aug 22, 2014

      @tebeka
      tebekamannequin
      on Aug 22, 2014
      MannequinAuthor
      More actions

      For zip file the help should probably be:
      usage: python file.zip

    5. tebeka commented on Aug 24, 2014

      @tebeka
      tebekamannequin
      on Aug 24, 2014
      MannequinAuthor
      More actions

      New patch with handling of zip files.

    6. paulj3 commented on Aug 24, 2014

      @paulj3
      paulj3mannequin
      on Aug 24, 2014
      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.

    7. paulj3 commented on Aug 24, 2014

      @paulj3
      paulj3mannequin
      on Aug 24, 2014
      Mannequin
      More actions

      What if the package is run without -m?

    8. tebeka commented on Aug 25, 2014

      @tebeka
      tebekamannequin
      on Aug 25, 2014
      MannequinAuthor
      More actions

      Made the test more robust by using sys.executable in the comparison.

    9. tebeka commented on Aug 25, 2014

      @tebeka
      tebekamannequin
      on Aug 25, 2014
      MannequinAuthor
      More actions

      How can you run a package without -m?

    10. paulj3 commented on Aug 25, 2014

      @paulj3
      paulj3mannequin
      on Aug 25, 2014
      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.

    11. ncoghlan commented on Aug 26, 2014

      @ncoghlan
      ncoghlan
      on Aug 26, 2014
      Contributor
      More actions

      If you have:

      <curdir>
      /subdir
      __main__.py

      Then in 3.3+, both of the following will work:

      python3 subdir
      python3 -m subdir
      

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

    12. tebeka commented on Aug 26, 2014

      @tebeka
      tebekamannequin
      on Aug 26, 2014
      MannequinAuthor
      More actions

      Support for directory invocation as well.

    13. tebeka commented on Sep 8, 2014

      @tebeka
      tebekamannequin
      on Sep 8, 2014
      MannequinAuthor
      More actions

      Anything else I need to solve to get this patch accepted?

    14. paulj3 commented on Sep 8, 2014

      @paulj3
      paulj3mannequin
      on Sep 8, 2014
      Mannequin
      More actions

      When I apply prog3.diff to my 3.5.0dev, and run

      python3 -m unittest [Lib/test/test_argparse.py](https://github.com/python/cpython/blob/main/Lib/test/test_argparse.py)
      

      I get 2 failures, both over 'usage: python3 -m [unit]test'.

      It also fails with

      python3 test_argparse.py
      

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

    15. 12 remaining items

    16. erlend-aasland
      moved this to Features in Argparse issueson May 19, 2022
    17. serhiy-storchaka
      self-assigned this
      on Sep 30, 2024
    18. serhiy-storchaka
      added
      3.14bugs and security fixesbugs and security fixes
      on Sep 30, 2024
    19. serhiy-storchaka
      added a commit that references this issue on Sep 30, 2024

      pythongh-66436: Improved prog default value for argparse.ArgumentParser

      5ba9a91
    20. serhiy-storchaka commented on Sep 30, 2024

      @serhiy-storchaka
      serhiy-storchaka
      on Sep 30, 2024
      Member
      More actions

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

      Reacted by Alyssa Coghlan
    21. serhiy-storchaka
      added a commit that references this issue on Sep 30, 2024

      pythongh-66436: Improved prog default value for argparse.ArgumentParser

      1977630
    22. serhiy-storchaka commented on Oct 1, 2024

      @serhiy-storchaka
      serhiy-storchaka
      on Oct 1, 2024
      Member
      More actions

      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.

    23. serhiy-storchaka
      added a commit that references this issue on Oct 1, 2024

      gh-66436: Improved prog default value for argparse.ArgumentParser (GH…

      04bfea2
    24. serhiy-storchaka
      closed this as completedon Oct 1, 2024
    25. github-project-automation
      moved this from Features to Doc issues in Argparse issueson Oct 1, 2024
    26. cjwatson
      added 2 commits that reference this issue on Dec 31, 2025

      fix: handle argparse changes in Python 3.14

      80aca04

      Handle argparse changes in Python 3.14

      c0a0d59
    27. bjoernricks
      added 3 commits that reference this issue on Jan 9, 2026

      test: Fix argument parser tests on Python 3.14

      79cbea1

      test: Fix argument parser tests on Python 3.14

      05b7ee5

      test: Fix argument parser tests on Python 3.14

      4170b0d
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    • @serhiy-storchaka
      serhiy-storchaka

    Labels

    3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    • Argparse issues
      Status
      Doc issues
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.