Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder5.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail 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; 'else:': 0.03; 'args': 0.04; '(python': 0.05; 'case.': 0.05; 'importerror:': 0.05; 'say,': 0.05; 'skip:" 60': 0.05; 'args)': 0.07; 'differently': 0.07; 'none)': 0.07; 'optparse': 0.07; 'try:': 0.07; 'python': 0.09; 'filesystem': 0.09; 'imports': 0.09; 'logic': 0.09; 'module)': 0.09; 'nameerror:': 0.09; 'outlined': 0.09; 'path.': 0.09; 'subject:command': 0.09; 'sys.argv[0]': 0.09; 'cc:addr :python-list': 0.10; '"python': 0.16; "'-m',": 0.16; '...]': 0.16; 'attribute,': 0.16; 'benjamin': 0.16; 'cases:': 0.16; 'cc:name:python list': 0.16; 'doing:': 0.16; 'dropping': 0.16; 'none"': 0.16; 'rerun': 0.16; 'script?': 0.16; 'sys.path': 0.16; 'string': 0.17; 'wrote:': 0.17; 'module,': 0.17; 'resolved': 0.17; 'tim': 0.18; '>>>': 0.18; 'module': 0.19; 'versions': 0.20; 'equivalent': 0.20; 'issue.': 0.20; 'written': 0.20; 'bit': 0.21; 'import': 0.21; 'meant': 0.21; 'implicit': 0.22; 'skip:_ 20': 0.22; 'cc:2**0': 0.23; 'branch': 0.23; 'split': 0.23; 'raise': 0.24; 'command': 0.24; 'script': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'looks': 0.26; 'skip:[ 10': 0.26; 'skip:" 20': 0.26; '(most': 0.27; '2.6': 0.27; 'instead.': 0.27; 'opposed': 0.27; 'skip:" 50': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'run': 0.28; 'cat': 0.29; 'equivalent.': 0.29; 'waters': 0.29; '(including': 0.30; "skip:' 10": 0.30; 'relative': 0.30; 'primary': 0.30; 'code': 0.31; 'file': 0.32; 'switch': 0.32; 'running': 0.32; 'could': 0.32; "aren't": 0.33; 'traceback': 0.33; 'version': 0.34; 'changed': 0.34; 'skip:b 20': 0.34; 'received:google.com': 0.34; 'wrong': 0.34; 'described': 0.35; 'false': 0.35; 'path': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; "didn't": 0.36; 'method': 0.36; 'too': 0.36; 'problems': 0.36; 'does': 0.37; 'option': 0.37; 'two': 0.37; 'rather': 0.37; 'received:209': 0.37; 'well.': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; 'little': 0.39; 'think': 0.40; 'matter': 0.61; 'places': 0.61; 'between': 0.63; 'more': 0.63; 'differences': 0.65; 'believe': 0.69; 'direct': 0.69; '2013': 0.84; 'case?': 0.84; 'oscar': 0.84; 'package?': 0.84; 'say:': 0.84; 'top.': 0.84; 'drops': 0.91; 'package"': 0.91; 'to:addr:mail': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=IiQmjyE0Ac1IRS6W5jzu2xoGgJnnV2URD5ayMpwq+WY=; b=o4U4saxH9zdDXLRVe/d7N9uQbYB7XJ9/VEn5oR34KXRkC1D4v/KnbHeMj2mLprZQhy +uTSy2zuH9rM/wDUHzkQB0oCe88hiNcfM80zYKm+FHpSAGIBz4y4xYUAtqR0JHgzpiSG DtnZVSfo+BLB/q9HIdkx8E6ht2ZcAnffjhQxu7+ZTajg9HTeXR9AabXXpbNJ1ohfufIv c6tkwJUqhWeFzhGaKib5LCZdXXPXGsy3wlCSTl3pk23uruvGrQedln25O/BJiiMzmd+l 4cGuFGHopNs2BWU7u5Bv9lsVtiehzhdH4+yUkTbeC2VvpsTDq9NUFZrHBKyhqKg+w0Hk xvDQ== MIME-Version: 1.0 X-Received: by 10.152.109.210 with SMTP id hu18mr2954424lab.12.1359057697006; Thu, 24 Jan 2013 12:01:37 -0800 (PST) In-Reply-To: <51016BA5.6010401@timgolden.me.uk> References: <50FE5AC7.3050909@timgolden.me.uk> <50ff24e8$0$29994$c3e8da3$5496439d@news.astraweb.com> <50ff5ffc$0$29877$c3e8da3$5496439d@news.astraweb.com> <5100bd49$0$29877$c3e8da3$5496439d@news.astraweb.com> <51011340.6020309@timgolden.me.uk> <51013B0C.6000309@timgolden.me.uk> <51015885.9060104@timgolden.me.uk> <51016BA5.6010401@timgolden.me.uk> Date: Thu, 24 Jan 2013 20:01:36 +0000 Subject: Re: Retrieving the full command line From: Oscar Benjamin To: Tim Golden Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 101 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359057699 news.xs4all.nl 6990 [2001:888:2000:d::a6]:39639 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37623 On 24 January 2013 17:13, Tim Golden wrote: > On 24/01/2013 16:53, Oscar Benjamin wrote: >>> Does it work if you use the -m option to run a module rather than a script? >> >> Sorry that was written incorrectly. I meant to say: does it work when >> a module is directly on sys.path rather than as a submodule of a >> package? In this case __package__ is set to the empty string if run >> with -m or None if run with a direct path. So the check needs to be >> "__package__ is not None" rather than "bool(__package__)". > > The answer is: it depends. Given the code I outlined earlier: > > A package-based module run via -m (python -m package.module) works > as described (including the implicit __main__ module, my > primary use-case). Does it work in the "python -m package.module" case? It looks to me as if the code below will run "python -m package" instead. I think you need to split the module name out of sys.argv[0] and put that into the command line as well. > import __main__ > > # [.. .snip ...] > > try: > is_package = bool(__main__.__package__) > except NameError: > is_package = False > if is_package: > args = [sys.executable, '-m', __main__.__package__] + sys.argv[1:] > else: > args = [sys.executable] + sys.argv > > os.chdir(_startup_cwd) # avoids relative/absolute issues > os.execv(args[0], args) I believe "python -m package" and "python -m package.__main__" are equivalent so it doesn't matter in that case. > > A module run from the filesystem (python c:\path\to\module.py) works > by dropping through through to the not is_package logic branch. > > A module run via -m (python -m module) actually works by accident, > because it too drops through to the not is_package branch and is > rerun with its full filesystem path. This doesn't have the same > problems as running a package from the filesystem because relative > imports aren't an issue. I don't know if there are any other differences > between python -mmodule and python c:\path\to\module.py. There is a (probably pathological) case in which running the script via a file path and running it via -m are not equivalent. "python dir/script.py" places "dir" at the top of sys.path. "python -m script" only works if "dir" is in sys.path but it needn't be at the top. This means that the order of sys.path is changed and import conflicts may be resolved differently in the two cases: ~$ cat ~/.local/lib/python2.7/site-packages/script.py print("Running script.py") import optparse ~$ cat ~/.local/lib/python2.7/site-packages/optparse.py raise ImportError('Wrong optparse!') ~$ python ~/.local/lib/python2.7/site-packages/script.py Running script.py Traceback (most recent call last): File "/home/oscar/.local/lib/python2.7/site-packages/script.py", line 2, in import optparse File "/home/oscar/.local/lib/python2.7/site-packages/optparse.py", line 1, in raise ImportError('Wrong optparse!') ImportError: Wrong optparse! ~$ python -m script Running script.py ~$ python ~/.local/lib/python2.7/site-packages/script.py Running script.py Traceback (most recent call last): File "/home/oscar/.local/lib/python2.7/site-packages/script.py", line 2, in import optparse File "/home/oscar/.local/lib/python2.7/site-packages/optparse.py", line 1, in raise ImportError('Wrong optparse!') ImportError: Wrong optparse! ~$ python -m script Running script.py > > As you say, a more refined check could determine a blank __package__ > as opposed to a None __package__. But this code (cherrypy) must also > cope with version of Python before 2.6 which didn't even have a > __package__ attribute, muddying the waters that little bit further. Then you won't be able to use this method to get the -m switch to work on Python < 2.6. If it's ok to just never try the -m switch on those versions then it's as simple as doing: pkg = getattr(__main__, '__package__', None) Oscar