Inspired by another question here, I would like to retrieve the Python interpreter's full command line in a portable way. That is, I want to get the original argv
of the interpreter, not the sys.argv
which excludes options to the interpreter itself (like -m
, -O
, etc.).
sys.flags
tells us which boolean options were set, but it doesn't tell us about -m
arguments, and the set of flags is bound to change over time, creating a maintenance burden.
On Linux you can use procfs to retrieve the original command line, but this is not portable (and it's sort of gross):
open('/proc/{}/cmdline'.format(os.getpid())).read().split('\0')
*argc
and**argv
. There is Py_GetArgcArgv, that you could probably hook into -- But I don't see it anywhere in the documented C-API... – Algia.split('\0')
would be more correct than.replace('\0', ' ')
-- otherwise you cannot distinguish between arguments containing a space and separate arguments. – Tinhorn