【发布时间】:2020-09-14 19:18:56
【问题描述】:
有没有办法从另一个 Python (Python 3) 脚本以优化模式运行 Python 脚本?
如果我有以下test.py 脚本(读取built-in constant __debug__):
if __debug__:
print('Debug ON')
else:
print('Debug OFF')
然后:
-
python text.py打印Debug ON -
python -OO text.py打印Debug OFF
因为constant __debug__ 的工作原理:
如果 Python 没有以
-O选项启动,则此常量为真。另见assert声明。
此外,__debug__ 的值不能在运行时更改:__debug__ 是一个常量,如文档here 和here 中所述。 __debug__ 的值是在 Python 解释器启动时确定的。
以下正确打印 Debug OFF
import subprocess
subprocess.run(["python", "-OO", "test.py"])
但是有没有更多的 pythonic 方式?
如果解释器不被称为python,上述内容似乎不太便携。
我已经在这里和网上搜索过,但没有运气。
【问题讨论】:
标签: python python-3.x subprocess compiler-optimization assert