Retrieving the full command line

Oscar Benjamin oscar.j.benjamin at
Thu Jan 24 11:06:37 CET 2013

On 24 January 2013 04:49, Steven D'Aprano
<steve+comp.lang.python at> wrote:
> Contrariwise, I don't believe that there is currently *any* way to
> distinguish between running a script with or without -m. That should be
> fixed.

As I said earlier in the thread, the __package__ module global
distinguishes the two cases:

~$ mkdir pkg
~$ touch pkg/
~$ vim pkg/
~$ cat pkg/
import sys
if __package__ is None:
    cmdline = [sys.executable] + sys.argv
    cmdline = [sys.executable, '-m', __package__] + sys.argv[1:]
~$ python pkg/ arg1 arg2
['q:\\tools\\Python27\\python.exe', 'pkg/', 'arg1', 'arg2']
~$ python -m pkg arg1 arg2
['q:\\tools\\Python27\\python.exe', '-m', 'pkg', 'arg1', 'arg2']


More information about the Python-list mailing list