This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Import command-line parsing modules only when needed
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.6
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: aronacher, berker.peksag, bethard, iritkatriel, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2017-04-26 08:34 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 1293 merged serhiy.storchaka, 2017-04-26 08:36
PR 2098 merged terry.reedy, 2017-06-11 04:16
Messages (5)
msg292319 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-04-26 08:34
Berker suggested to move this part from issue30152 to its own PR.

When the file can be imported as a module and run as a script it is worth to make command-line parsing modules (getopt, optparse, argparse) be imported only when they are used, i.e. when the file is ran as a script. Most of the stdlib modules already do this.

Proposed patch moves imports of command-line parsing modules and some other modules used only when the module is ran to the main() function or to the branch executed only if __name__ == "__main__". It doesn't change scripts and files that are purposed to be used only for running (,
msg292961 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-05-04 05:17
New changeset 7e4db2f253c555568d56177c2fd083bcf8f88d34 by Serhiy Storchaka in branch 'master':
bpo-30166: Import command-line parsing modules only when needed. (#1293)
msg292965 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-05-04 06:16
I want to backport the idlelib.pyshell change.  I have been told that even a partial backport, even if done by hand, is a backport and the result a cherry-pick.
msg295687 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-06-11 04:34
New changeset c0ef607c52e58e94693fbedb27f2813bc3fed8fa by terryjreedy in branch '3.6':
[3.6] bpo-30166: backport pyshell changes (GH 1293) (#2098)
msg377159 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-09-19 11:33
Looks like the backport was done and this issue can be closed.
Date User Action Args
2022-04-11 14:58:45adminsetgithub: 74352
2020-09-19 15:36:00serhiy.storchakasetstatus: open -> closed
stage: backport needed -> resolved
2020-09-19 11:33:58iritkatrielsetnosy: + iritkatriel
messages: + msg377159
2017-06-11 04:34:22terry.reedysetmessages: + msg295687
2017-06-11 04:16:24terry.reedysetpull_requests: + pull_request2154
2017-05-04 06:16:19terry.reedysetstatus: closed -> open

assignee: serhiy.storchaka -> terry.reedy
versions: + Python 3.6, - Python 3.7
nosy: + terry.reedy

messages: + msg292965
stage: resolved -> backport needed
2017-05-04 05:53:31serhiy.storchakasetstatus: open -> closed
assignee: serhiy.storchaka
resolution: fixed
stage: patch review -> resolved
2017-05-04 05:17:50serhiy.storchakasetmessages: + msg292961
2017-04-26 08:38:00serhiy.storchakasetnosy: + bethard, aronacher, berker.peksag
2017-04-26 08:36:46serhiy.storchakasetpull_requests: + pull_request1400
2017-04-26 08:34:19serhiy.storchakacreate