5

A command line calling a python program looks something like:

$ python [python_options] myprogram.py [args]

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

I don't use python_options a lot, but sometimes it's useful, e.g. -u (unbuffered output) or -3 (check for python3 incompatibilities).

To be precise, I want to create a subprocess which is another python program, and I want to pass it the same python_options. (I know about sys.flags, but that's not what I want. I don't want the values of the flags; I want the actual string used in the command line which sets those flags).

Peter B
  • 453
  • 4
  • 15
  • Maybe digging through the code of `multiprocessing` would help? I'd assume it passes the flags to its worker processes. – kichik Apr 28 '17 at 01:26
  • I'm beginning to think this can't be done directly, and instead I need to do this: `$ python [python_options] myprogram.py [python_options2] [args]`, and pass `python_options2` to my subprocess. Obviously I need to ensure myprogram.py doesn't confuse the `args` and python options with each other, but I think I can do that. – Peter B Apr 28 '17 at 04:20
  • It might be better if you write another script accepts `python_options` as arguments and treat `myprogram.py` as a special subprocess. And in that case `python_options2` is never needed. Hope it helps you :) – Roll Apr 28 '17 at 04:57
  • You can use ctypes to get the original command-line arguments and keep everything up to `[-c cmd | -m mod | file | -]`. – Eryk Sun Apr 28 '17 at 05:28
  • @eryksun How would I do that? (I've had a look at the ctypes page but can't see anything there to help). – Peter B May 03 '17 at 00:09
  • `import ctypes;` `argc = ctypes.c_int();` `argv = ctypes.POINTER(ctypes.c_char_p)();` `ctypes.pythonapi.Py_GetArgcArgv(ctypes.byref(argc), ctypes.byref(argv));` `argv = argv[:argc.value]`. In Python 3 use `ctypes.c_wchar_p` instead. – Eryk Sun May 03 '17 at 00:33
  • Thank you. That works on my simple test case. You should submit that as answer. – Peter B May 03 '17 at 00:44
  • It's the first step in a complete answer, which could maybe use [getopt](https://docs.python.org/2/library/getopt.html) to reproduce how the interpreter parses the command line. – Eryk Sun May 03 '17 at 03:03

2 Answers2

0

One possible solution is to generate 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 flag -R should be removed here if the version of your python is lower than 2.7.3.

Roll
  • 289
  • 1
  • 5
  • Curiously, the state of -u isn't available in sys.flags. – Peter B Apr 28 '17 at 02:54
  • @PeterB I just found that `-u` is used to [disable output buffering](http://stackoverflow.com/questions/107705/disable-output-buffering) while `-U` if for _Unicode_. – Roll Apr 28 '17 at 03:30
0

in a easy way, first install cli-args-system with

windows: pip install cli-args-system 
linux:  pip3 install cli-args-system 

after

from cli_args_system import Args
args = Args()
flags = args.flags()
print(flags)
  • Judging from the documentation of cli-args-system, it looks like it just works with `sys.argv`, rather than accessing the options passed to the Python interpreter *itself*. It's the latter that the question was asking about. – jjramsey Jan 05 '22 at 15:32
  • no, it has a lot of features, but, try to run this code , you will see it capture all the flags, the dock is here: https://pypi.org/project/cli-args-system/ (i did this lib) – Mateus Moutinho Jan 05 '22 at 15:45
  • I saw that documentation, and none of the examples have any options *directly* after `python3`, e.g., the `-E` option in `python3 -E test.py -a 1 -b 2`. The options are all for the `test.py` script. – jjramsey Jan 05 '22 at 15:56
  • I dont understant what is these, may i confused this with flags, in this case, the lib doesnt cover these – Mateus Moutinho Jan 05 '22 at 16:01