How do I access python command line options (not args)

The command line that invokes the python program looks something like this:

$ python [python_options] myprogram.py [args]

I know I can access args (sys.argv), but how do I access python_options?

I do not use python_options much, but sometimes it is useful, for example. -u (unbuffered output) or -3 (check for python3 incompatibility).

To be precise, I want to create a subprocess that is another python program, and I want to pass the same python_options to it. (I know about sys.flags, but thatโ€™s not what I want. I donโ€™t need the values โ€‹โ€‹of the flags, I want the actual line to be used on the command line that sets these flags).

+5
source share
1 answer

One possible solution is to create the flag string manually.

import sys def getFlags(): flags = ['-d', '-3', '-Q', '-Qnew', '-i', '-i', '-O', '-B', '-s', '-S', '-E', '-t', '-v', '-U', '-b', '-R'] return ' '.join({s for s, f in zip(flags, sys.flags) if f}) print getFlags() 

See Python Docs for sys.flags

EDIT: the -R flag should be removed here if your python version is below 2.7.3 .

0
source

Source: https://habr.com/ru/post/1267255/


All Articles