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.

6
  • You may need to use argparse. Commented Sep 14, 2019 at 20:04
  • 1
    @Hiadore can you be more specific - which function in argparse? Commented Sep 14, 2019 at 20:09
  • 2
    Possible duplicate of How to get python interpreter full argv command line options? Commented Sep 14, 2019 at 20:14
  • 2
    Please see this answer: stackoverflow.com/a/57914236/5588279 Commented Sep 14, 2019 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. Commented Sep 14, 2019 at 20:19

1 Answer 1

1

You can use sys.argv

example:

C:\Users\Rick>python -c "from sys import argv;print(argv)"
['-c']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.