Retrieving the full command line

Tim Golden mail at
Thu Jan 24 21:54:44 CET 2013

On 24/01/2013 20:01, Oscar Benjamin wrote:
> On 24 January 2013 17:13, Tim Golden <mail at> wrote:
>> 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.

Good catch. Unless I am, once again, mistaken, I can't see any way
of recreating a dotted module path which is running as a main module
without the somewhat fragile expedient of walking back up the
filepath and hoping that nothing strange has happened to the imports
on the way down? (pywin32: I'm looking at you!)

And we haven't even touched on zipped archives or other invented
loaders (pulling modules from a database or direct from the internet).

I think, though, that the point which with I started this thread is
still valid: that to reconstruct the command line with which Python
was started, you need an increasingly awkward set of conditions and
checks. As opposed to asking the interpreter: please give me the command
line which started you.

How common that particular requirement actually is, I couldn't say.
But the two cases which clearly could use it are: the reloader
setup we've been discussing here; and the apply-new-version-and-restart
which I'm hoping to use in another setup at work.

Thanks for all the help and input on this, Oscar


More information about the Python-list mailing list