如何禁用python警告

本文翻译自:How to disable python warnings

I am working with code that throws a lot of (for me at the moment) useless warnings using the warnings library. 我正在使用使用warnings库抛出很多(目前对我而言)无用警告的代码。 Reading (/scanning) the documentation I only found a way to disable warnings for single functions . 阅读(/扫描)文档后,我只找到了一种禁用单个功能警告的方法 But I don't want to change so much of the code. 但是我不想更改太多代码。

Is there maybe a flag like python -no-warning foo.py ? 是否有像python -no-warning foo.py这样的标志?

What would you recommend? 你会推荐什么?


#1楼

参考:https://stackoom.com/question/ygYf/如何禁用python警告


#2楼

-W选项

python -W ignore foo.py


#3楼

Did you look at the suppress warnings section of the python docs? 您是否看过python文档的“ 抑制警告”部分?

If you are using code that you know will raise a warning, such as a deprecated function, but do not want to see the warning, then it is possible to suppress the warning using the catch_warnings context manager: 如果您正在使用知道会发出警告的代码(例如已弃用的函数),但又不想看到该警告,则可以使用catch_warnings上下文管理器来抑制该警告:

import warnings

def fxn():
    warnings.warn("deprecated", DeprecationWarning)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    fxn()

I don't condone it, but you could just suppress all warnings with this: 我不宽容,但是您可以通过以下方式禁止所有警告

import warnings
warnings.filterwarnings("ignore")

Ex: 例如:

>>> import warnings
>>> def f():
...  print('before')
...  warnings.warn('you are warned!')
...  print('after')
>>> f()
before
__main__:3: UserWarning: you are warned!
after
>>> warnings.filterwarnings("ignore")
>>> f()
before
after

#4楼

You can also define an environment variable (new feature in 2010 - ie python 2.7) 您还可以定义环境变量(2010年的新功能-即python 2.7)

export PYTHONWARNINGS="ignore"

Test like this: Default 像这样测试: 默认

$ export PYTHONWARNINGS="default"
$ python
>>> import warnings
>>> warnings.warn('my warning')
__main__:1: UserWarning: my warning
>>>

Ignore warnings 忽略警告

$ export PYTHONWARNINGS="ignore"
$ python
>>> import warnings
>>> warnings.warn('my warning')
>>> 

For deprecation warnings have a look at how-to-ignore-deprecation-warnings-in-python 对于弃用警告,请查看如何忽略python中的弃用警告

Copied here... 在这里复制...

From documentation of the warnings module : warnings模块的文档中:

 #!/usr/bin/env python -W ignore::DeprecationWarning

If you're on Windows: pass -W ignore::DeprecationWarning as an argument to Python. 如果您在Windows上:将-W ignore::DeprecationWarning作为Python的参数传递。 Better though to resolve the issue, by casting to int . 最好通过强制转换为int来解决问题。

(Note that in Python 3.2, deprecation warnings are ignored by default.) (请注意,在Python 3.2中,默认情况下会忽略弃用警告。)

Or: 要么:

import warnings

with warnings.catch_warnings():
    warnings.filterwarnings("ignore",category=DeprecationWarning)
    import md5, sha

yourcode()

Now you still get all the other DeprecationWarning s, but not the ones caused by: 现在,您仍然可以获得所有其他的DeprecationWarning ,但不是由以下原因引起的:

import md5, sha

#5楼

warnings are output via stderr and the simple solution is to append '2> /dev/null' to the CLI. 警告通过stderr输出,简单的解决方案是将'2> / dev / null'附加到CLI。 this makes a lot of sense to many users such as those with centos 6 that are stuck with python 2.6 dependencies (like yum) and various modules are being pushed to the edge of extinction in their coverage. 这对许多用户来说非常有意义,例如那些使用python 2.6依赖项(例如yum)的centos 6的用户,并且各种模块的使用范围已被淘汰。

this is especially true for cryptography involving SNI et cetera. 对于涉及SNI等的密码术尤其如此。 one can update 2.6 for HTTPS handling using the proc at: https://urllib3.readthedocs.io/en/latest/user-guide.html#ssl-py2 可以使用以下过程中的proc更新2.6以进行HTTPS处理: https : //urllib3.readthedocs.io/en/latest/user-guide.html#ssl-py2

the warning is still in place, but everything you want is back-ported. 警告仍然存在,但是您想要的所有内容都已反向移植。 the re-direct of stderr will leave you with clean terminal/shell output although the stdout content itself does not change. 尽管stdout内容本身不会更改,但stderr的重定向将为您提供干净的终端/外壳输出。

responding to FriendFX. 回应FriendFX。 sentence one (1) responds directly to the problem with an universal solution. 句子(1)用通用解决方案直接回答了这个问题。 sentence two (2) takes into account the cited anchor re 'disable warnings' which is python 2.6 specific and notes that RHEL/centos 6 users cannot directly do without 2.6. 句子二(2)考虑到了python 2.6特定的引用的anchor re'disable warnings',并指出RHEL / centos 6用户不能直接使用2.6。 although no specific warnings were cited, para two (2) answers the 2.6 question I most frequently get re the short-comings in the cryptography module and how one can "modernize" (ie, upgrade, backport, fix) python's HTTPS/TLS performance. 尽管未引用任何具体的警告,但是第(2)款回答了我最经常得到的2.6问题,即密码模块的缺点以及如何“现代化”(即升级,反向移植,修复)python的HTTPS / TLS性能。 para three (3) merely explains the outcome of using the re-direct and upgrading the module/dependencies. 第三(3)段仅说明使用重定向和升级模块/依赖项的结果。


#6楼

This is an old question but there is some newer guidance in PEP 565 that to turn off all warnings if you're writing a python application you should use: 这是一个老问题,但是PEP 565中有一些较新的指南,如果您正在编写python应用程序,则应关闭所有警告:

import sys
import warnings

if not sys.warnoptions:
    warnings.simplefilter("ignore")

The reason this is recommended is that it turns off all warnings by default but crucially allows them to be switched back on via python -W on the command line or PYTHONWARNINGS . 推荐这样做的原因是,默认情况下它会关闭所有警告,但至关重要的是允许它们通过命令行或PYTHONWARNINGS上的python -W重新打开。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值