简体   繁体   中英

How do you suppress Python DeprecationWarnings on Linux Terminal?

I installed i18ndude (an internationalization utility to be used in Plone) using easy_install .

When I try to run the utility i18ndude on my terminal, I get:

/usr/local/lib/python2.6/dist-packages/i18ndude-3.1.2-py2.6.egg/i18ndude/odict.py:7: DeprecationWarning: object.__init__() takes no parameters
  dict.__init__(self, dict)

How do I suppress these warning messages when calling the utility from command line? Is it possible? I know in theory I should install other Python interpreter, and call i18ndude from that, but I would like a simpler approach (like a parameter or something like that).

BTW, I'm using a i18ndude script from Plone official site .

Redirection can be used, but it would suppress all the messages sent to that "stream"; eg

i178ndude 2>/dev/null

sends to the null device the stream 2 (normally the stderr of a program, but deprecation warnings could be sent to other streams). This is the "fix it even though you don't know how" fix. Indeed there's an option, -W, that can be used like this: -W ignore::DeprecationWarning or simply -W ignore that ignores all warnings. You can write a script that call the python interpreter on your program, or more logically modify the #! of the prog with something like #!/usr/bin/env python -W ignore::DeprecationWarning

如果作为脚本运行,您可以使用:

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

You can temporarily suppress warnings :

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:

import warnings

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

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

While within the context manager all warnings will simply be ignored. This allows you to use known-deprecated code without having to see the warning while not suppressing the warning for other code that might not be aware of its use of deprecated code. Note: this can only be guaranteed in a single-threaded application. If two or more threads use the catch_warnings context manager at the same time, the behavior is undefined.

See cmdoption-W :

-W arg

Warning control. Python's warning machinery by default prints warning messages to sys.stderr. A typical warning message has the following form:

file:line: category: message

By default, each warning is printed once for each source line where it occurs. This option controls how often warnings are printed.

Multiple -W options may be given; when a warning matches more than one option, the action for the last matching option is performed. Invalid -W options are ignored (though, a warning message is printed about invalid options when the first warning is issued).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM