2

How to programmatically retrieve the interpreter options passed to the python interpreter within that instance?

For example, given command

python -B -u script.py

what Python code lists the passed interpreter command-line options -B and -u?

I could query sys.dont_write_bytecode to infer that command-line option -B was passed. But I want Python code that is generic to any interpreter option. I imagine a code snippet that would return a list of command-line options the interpreter was given, e.g. [ '-B', '-u' ].


To question reviewers, this question is not answered in How to get python interpreter full argv command line options?. That answer for that question suggests changing the C-runtime code of the interpreter and recompiling Python. That is not a suitable solution for my needs.

JamesThomasMoon
  • 6,169
  • 7
  • 37
  • 63
  • You may need to use argparse. – Hiadore Sep 14 '19 at 20:04
  • 1
    @Hiadore can you be more specific - which function in `argparse`? – JamesThomasMoon Sep 14 '19 at 20:09
  • 2
    Possible duplicate of [How to get python interpreter full argv command line options?](https://stackoverflow.com/questions/28412903/how-to-get-python-interpreter-full-argv-command-line-options) – norok2 Sep 14 '19 at 20:14
  • 2
    Please see this answer: https://stackoverflow.com/a/57914236/5588279 – Sraw Sep 14 '19 at 20:17
  • 2
    Btw, I want to point out that this an implementation-specific problem. So the solution won't work on the others other than C implementation. – Sraw Sep 14 '19 at 20:19
  • Possible duplicate of [Retrieve the command line arguments of the Python interpreter](https://stackoverflow.com/questions/28336431/retrieve-the-command-line-arguments-of-the-python-interpreter) – Ondrej K. Sep 14 '19 at 22:10

1 Answers1

1

You can use sys.argv

example:

C:\Users\Rick>python -c "from sys import argv;print(argv)"
['-c']
BIT-REAPER
  • 40
  • 8