[issue14208] No way to recover original argv with python -m

Nick Coghlan report at bugs.python.org
Wed Mar 7 02:38:34 CET 2012


Nick Coghlan <ncoghlan at gmail.com> added the comment:

This is closely related to PEP 395, since multiprocessing currently hits the same issue in trying to figure out the correct setting for sys.argv0.

I quite like the sys.__argv__ idea for preserving the *exact* underlying command line (Alex Gaynor was recently asking for something similar to this).

In the meantime, it should be possible to work around the problem by running the affected subprocess invocations (i.e. when __main__.__package__ exists and is not empty) with something like:

    launch_template = """
    import runpy, sys
    sys.argv[:] = {argv}
    sys.path[:] = {path}
    runpy._run_module_as_main({main})
    """
    import sys, subprocess, os.path, __main__
    main_base = os.path.basename(__main.__file__).splitext()[0]
    main_ref = __main__.__package__ + "." + main_base
    launch = launch_template.format(argv=sys.argv, path=sys.path, main=main_ref)
    subprocess.call(launch, shell=True, executable=sys.executable)
   
Note: the above isn't tested code, since it's an approach that only occurred to me recently and I haven't had a chance to fully explore it myself. However, if it works, we could make use of it in 2.7 and 3.2 to fix multiprocessing's current misbehaviour on Windows.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14208>
_______________________________________


More information about the Python-bugs-list mailing list