Berker suggested to move this part from 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 (__main__.py, main.py).
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.