Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37389 > unrolled thread

Re: Retrieving the full command line

Started byOscar Benjamin <oscar.j.benjamin@gmail.com>
First post2013-01-23 01:14 +0000
Last post2013-01-23 01:14 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Retrieving the full command line Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-01-23 01:14 +0000

#37389 — Re: Retrieving the full command line

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-01-23 01:14 +0000
SubjectRe: Retrieving the full command line
Message-ID<mailman.852.1358903657.2939.python-list@python.org>
On 22 January 2013 09:24, Tim Golden <mail@timgolden.me.uk> wrote:
> [Python 2.7/3.3 (and hg tip) running on Windows. Not Windows-specific,
> though].
>
> I use the python -mpackage incantation to run a package which has a
> __main__.py module and which uses relative imports internally.
>
> I'm developing under cherrypy which includes a reloader for development.
> The reloader attempts to rebuild the original
> command line by combining sys.executable and sys.argv and then does an
> execv.
>
> There does not appear to be any way within Python of determining the
> command line I used. The combination of sys.executable and sys.argv in
> this case will look like: "c:\python33\python.exe app/__main__.py". But
> running this precludes the use of package-relative imports.

I tried this on Linux but I imagine that it works the same on Windows.
You can check the value of the __package__ module global:

~$ mkdir tmp
~$ touch tmp/__init__.py
~$ vim tmp/__main__.py
~$ cat tmp/__main__.py
if __package__ is None:
    print 'Running as a script'
else:
    print 'Running with the -m option'
~$ python tmp/__main__.py
Running as a script
~$ python -m tmp
Running with the -m option


Oscar

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web