Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'debug': 0.04; '(python': 0.05; '__name__': 0.07; 'executed': 0.07; 'main()': 0.07; 'modifying': 0.07; 'problem?': 0.07; 'effect.': 0.09; 'implies': 0.09; 'imported': 0.09; 'imports': 0.09; 'instances.': 0.09; 'patch.': 0.09; 'shaw': 0.09; 'subject:module': 0.09; 'python': 0.10; '${1+"$@"}': 0.16; "'__main__':": 0.16; 'bind': 0.16; 'code?': 0.16; 'confusion': 0.16; 'distinct': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'himself.': 0.16; 'message- id:@cskk.homeip.net': 0.16; "module's": 0.16; 'silly': 0.16; 'simpson': 0.16; 'world;': 0.16; 'wrappers': 0.16; 'debugging': 0.18; 'hack': 0.18; 'shell': 0.18; 'all,': 0.20; 'exec': 0.22; 'function,': 0.22; 'cheers,': 0.22; 'trying': 0.22; 'passing': 0.23; 'this:': 0.23; 'import': 0.24; 'thus': 0.24; 'module': 0.25; "i've": 0.25; 'header:User-Agent:1': 0.26; 'command': 0.26; 'module.': 0.27; 'invoke': 0.29; 'short,': 0.29; 'code:': 0.29; 'typically': 0.29; 'program,': 0.29; 'code': 0.30; 'aside': 0.32; 'maybe': 0.33; 'message.': 0.33; 'purposes,': 0.33; 'i.e.': 0.35; 'instance': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'tool': 0.36; 'depends': 0.36; 'modules': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'being': 0.37; 'charset:us-ascii': 0.37; 'things': 0.38; 'progress': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'save': 0.60; 'real': 0.62; 'course': 0.62; 'world': 0.64; 'accessed': 0.66; 'cameron': 0.66; 'realise': 0.66; 'therefore': 0.67; 'received:61': 0.72; 'useful.': 0.72; 'directly.': 0.76; 'about,': 0.84; 'adapts': 0.84; 'bernard': 0.84; 'code):': 0.84; 'implications': 0.84; 'program!': 0.84; 'proposal:': 0.84; 'subject:official': 0.84; 'man.': 0.93 X-Authentication-Info: Submitted using ID cskk@bigpond.com X-Authority-Analysis: v=2.0 cv=EJGEIilC c=1 sm=1 a=tWFDEGMdDmdK94EPRMUQSg==:17 a=vrnE16BAAAAA:8 a=ZtCCktOnAAAA:8 a=yEdEr6MRgwAA:10 a=uRRa74qj2VoA:10 a=pKOXUDeuR5PYMX7hqLcA:9 a=CjuIK1q_8ugA:10 a=tWFDEGMdDmdK94EPRMUQSg==:117 Date: Sun, 2 Aug 2015 13:53:27 +1000 From: Cameron Simpson To: python-list@python.org Subject: __main__ vs official module name: distinct module instances MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1438488073 news.xs4all.nl 2826 [2001:888:2000:d::a6]:43216 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.stben.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:94885 Hi All, Maybe this should be over in python-ideas, since there is a proposal down the bottom of this message. But first the background... I've just wasted a silly amount of time debugging an issue that really I know about, but had forgotten. I have a number of modules which include a main() function, and down the bottom this code: if __name__ == '__main__': sys.exit(main(sys.argv)) so that I have a convenient command line tool if I invoke the module directly. I typically have tiny shell wrappers like this: #!/bin/sh exec python -m cs.app.maildb -- ${1+"$@"} In short, invoke this module as a main program, passing in the command line arguments. Very useful. My problem? When invoked this way, the module cs.app.maildb that is being executed is actually the module named "__main__". If some other piece of code imports "cs.app.maildb" they get a _different_ instance of the module. In the same program! And how did it cause me trouble? I am monkey patching my module for debug purposes, and that monkey patcher imports the module by name. So I was monkey patching cs.app.maildb, and _not_ patching __main__. And thus not seeing any effect from the patch. I realise that having __name__ == '__main__' at all almost implies this effect. I am not sure it needs to. The Proposal: What are the implications of modifying the python invocation: python -m cs.app.maildb to effectively do this (Python pseudo code): M = importlib.import("cs.app.maildb") M.__name__ = '__main__' sys.modules['__main__'] = M i.e. import the module by name, but bind it to _both_ "cs.app.maildb" and "__main__" in sys.modules. And of course hack .__name__ to support the standard boilerplate. This would save some confusion when the module is invoked from the python command line and also imported by the code; it is not intuitive that those two things give you distinct module instances. Aside from the module's .__name__ being '__main__' even when accessed by the code as cs.app.maildb, are there other implications to such a change that would break real world code? Cheers, Cameron Simpson The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man. - George Bernard Shaw