Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37389
| References | <50FE5AC7.3050909@timgolden.me.uk> |
|---|---|
| Date | 2013-01-23 01:14 +0000 |
| Subject | Re: Retrieving the full command line |
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.852.1358903657.2939.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Retrieving the full command line Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-01-23 01:14 +0000
csiph-web