How to disable python warnings?

前端 未结 8 1028
醉梦人生
醉梦人生 2020-11-22 04:24

I am working with code that throws a lot of (for me at the moment) useless warnings using the warnings library. Reading (/scanning) the documentation I only found a way to d

相关标签:
8条回答
  • 2020-11-22 04:30

    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:

    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.

    0 讨论(0)
  • 2020-11-22 04:32

    Did you look at the suppress warnings section of the python docs?

    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()
    

    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
    
    0 讨论(0)
  • 2020-11-22 04:41

    You can also define an environment variable (new feature in 2010 - i.e. 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

    Copied here...

    From documentation of the warnings module:

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

    If you're on Windows: pass -W ignore::DeprecationWarning as an argument to Python. Better though to resolve the issue, by casting to int.

    (Note that in Python 3.2, deprecation warnings are ignored by default.)

    Or:

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

    Now you still get all the other DeprecationWarnings, but not the ones caused by:

    import md5, sha
    
    0 讨论(0)
  • 2020-11-22 04:41

    I realise this is only applicable to a niche of the situations, but within a numpy context I really like using np.errstate:

    np.sqrt(-1)
    
    __main__:1: RuntimeWarning: invalid value encountered in sqrt
    nan
    

    However, using np.errstate:

    with np.errstate(invalid='ignore'):
        np.sqrt(-1)
    
    nan
    

    The best part being you can apply this to very specific lines of code only.

    0 讨论(0)
  • 2020-11-22 04:42

    If you don't want something complicated, then:

    import warnings
    warnings.filterwarnings("ignore", category=FutureWarning)
    
    0 讨论(0)
  • 2020-11-22 04:51

    warnings are output via stderr and the simple solution is to append '2> /dev/null' to the 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.

    this is especially true for cryptography involving SNI et cetera. one can update 2.6 for HTTPS handling using the proc at: 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.

    responding to FriendFX. sentence one (1) responds directly to the problem with an universal solution. 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. 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" (i.e., upgrade, backport, fix) python's HTTPS/TLS performance. para three (3) merely explains the outcome of using the re-direct and upgrading the module/dependencies.

    0 讨论(0)
提交回复
热议问题