设置“PYTHONWARNINGS”以禁用python警告似乎没有任何作用

2024-05-18 19:14:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在运行openstack可执行文件,它会生成python弃用警告

经过一些搜索,我确实找到了thishowto

这里是relevant part

Use the PYTHONWARNINGS Environment Variable to Suppress Warnings in Python

We can export a new environment variable in Python 2.7 and up. We can export PYTHONWARNINGS and set it to ignore to suppress the warnings raised in the Python program.

然而,这样做:

PYTHONWARNINGS="ignore" openstack image show image name -f value -c id

不执行任何操作,仍会显示弃用警告

我已尝试将PYTHONWARNINGS设置为各种类型:

  • ignore
  • "ignore"
  • "all"
  • "deprecated"
  • "ignore::DeprecationWarning"
  • "error::Warning,default::Warning:has_deprecated_syntax"
  • "error::Warning"

但他们似乎什么也没做

我可以通过在末尾添加2>/dev/null来解决这个问题,但我想知道为什么PYTHONWARNINGS似乎什么都没做


Tags: andthetoinimage警告openstackexport
1条回答
网友
1楼 · 发布于 2024-05-18 19:14:06

PYTHONWARNINGS当然会抑制python的警告。尝试运行:

PYTHONWARNINGS="ignore" python -c "import warnings; warnings.warn('hi')"

但在本例中,您不是在调用python,而是在调用openstack,它显然没有继承相同的环境。不看消息来源我就说不出原因。它甚至可以显式地设置警告级别,这将覆盖您之前所做的任何操作

如果您不想看到错误,将STDERR发送到/dev/null是正确的方法

相关问题 更多 >

    热门问题