The original python command line

Damjan Georgievski gdamjan at
Sun Mar 4 06:52:26 CET 2012

> How can I get the *really* original command line that started my python
> interpreter?
> Werkzeug has a WSGI server which reloads itself when files are changed
> on disk. It uses `args = [sys.executable] + sys.argv` to kind of
> recreate the command line, and the uses to run that
> command line.
> BUT that's problematic as, when you run::
> 	python -m mypackage --config
> sys.argv printed in mypackage/ will be::
> 	['/full/path/to/mypackage/', '--config']
> so you get::
> 	python /full/path/to/mypackage/ --config
> instead of::
> 	python -m mypackage --config
> the difference in the 2 cases is what the current package is, and
> whether you can use relative imports.

BTW, the same thing happens in Python 3.2.2. To reproduce::

mkdir /tmp/X
cd /tmp/X

mkdir mypackage
touch  mypackage/ mypackage/

cat <<EOF > mypackage/
from __future__ import print_function, absolute_import
import os, sys, subprocess

def rerun():
    new_environ = os.environ.copy()
    new_environ['TEST_CHILD'] = 'true'
    args = [sys.executable] + sys.argv, env=new_environ)

if os.environ.get('TEST_CHILD') != 'true':
    Role = 'Parent'
    Role = 'Child'

    from . import dummy
    print('Exception in %s' % Role)
    print('Success in %s' % Role)

	python2 -m mypackage


	python3 -m mypackage

will print::

	Exception in Child
	Success in Parent

More information about the Python-list mailing list