【问题标题】:get the python command-line options passed to the python interpreter获取传递给 python 解释器的 python 命令行选项
【发布时间】:2020-01-16 05:27:57
【问题描述】:

如何以编程方式检索在该实例中传递给 python 解释器的解释器选项?

例如,给定命令

python -B -u script.py

哪些 Python 代码列出了传递的解释器命令行选项 -B-u

我可以查询sys.dont_write_bytecode 来推断命令行选项-B 已通过。但我想要对任何解释器选项都通用的 Python 代码。我想象一个代码 sn-p 将返回解释器给出的命令行选项列表,例如[ '-B', '-u' ].


对于审稿人来说,How to get python interpreter full argv command line options? 没有回答这个问题。该问题的答案建议更改解释器的 C 运行时代码并重新编译 Python。这不适合我的需求。

【问题讨论】:

标签: python


【解决方案1】:

你可以使用 sys.argv

示例:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    相关资源
    最近更新 更多