64

I am using Paramiko in my python code (for sftp). Everything works fine except that everytime I import or call a paramiko function. This warning would show up:

C:\Python26\lib\site-packages\Crypto\Util\randpool.py:40: RandomPool_Deprecation
Warning: This application uses RandomPool, which is BROKEN in older releases.  S
ee http://www.pycrypto.org/randpool-broken
  RandomPool_DeprecationWarning)

I know that this has to do with the fact that Paramiko is using some Deprecated functionalities of PyCrypto.

My question is, is there a way to suppress this warning programmatically ? I have tried this:

warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='paramiko')

and even this:

warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='randpool')

before 'import paramiko' statement and before paramiko-specific function calls, but nothing works. This warning keeps showing up no matter what. If it helps, here's the code in the third party library that prints the warning:

in randpool.py:

from Crypto.pct_warnings import RandomPool_DeprecationWarning
import Crypto.Random
import warnings

class RandomPool:
    """Deprecated.  Use Random.new() instead.

    See http://www.pycrypto.org/randpool-broken
    """
    def __init__(self, numbytes = 160, cipher=None, hash=None, file=None):
        warnings.warn("This application uses RandomPool, which is BROKEN in older releases.  See http://www.pycrypto.org/randpool-broken",
            RandomPool_DeprecationWarning)

If you know a way around this, please help me shut this warning off.

Tom Nguyen
  • 641
  • 1
  • 5
  • 3

4 Answers4

66

Easiest way would be as the warnings module suggests here:

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    import paramiko
snapshoe
  • 13,454
  • 1
  • 24
  • 28
27

The module argument to warnings.filterwarnings takes a case-sensitive regular expression which should match the fully qualified module name, so

warnings.filterwarnings(
    action='ignore',
    category=DeprecationWarning,
    module=r'.*randpool'
)

or

warnings.filterwarnings(
    action='ignore',
    category=DeprecationWarning,
    module=r'Crypto\.Utils\.randpool'
)

should work. You may need to write RandomPool_DeprecationWarning explicitly instead of DeprecationWarning if for some reason RandomPool_DeprecationWarning is not a subclass of DeprecationWarning.

You can also disable the warning on the command line when you invoke the script by passing the -W option to the interpreter like so:

$ python -W ignore::RandomPool_DeprecationWarning:Crypto.Utils.randpool: my_script.py

The -W takes filters in the format action:message:category:module:lineno, where this time module must exactly match the (fully-qualified) module name where the warning is raised.

See https://docs.python.org/2/library/warnings.html?highlight=warnings#the-warnings-filter and https://docs.python.org/2/using/cmdline.html#cmdoption-w

Blaine Rogers
  • 523
  • 4
  • 10
23

To filter only a specific warning:

with warnings.catch_warnings():
    warnings.simplefilter('ignore', SpecificWarningObject)

    #do something that raises a Warning
Kyle
  • 2,814
  • 2
  • 17
  • 30
  • It has been a while, but I still wonder (and don't have a good answer): how do I get the 'SpecificWarningObject' object? – tim654321 Apr 13 '23 at 05:17
  • Here, SpecificWarningObject is just a placeholder for whatever warning you want to filter. Do something like `from mymodule import SpecificWarningObject` and replace "SpecificWarningObject" with the name of the warning you want to ignore. – Kyle Apr 14 '23 at 15:29
  • Ah yes, I get the part about subbing in the object - but I have no idea how to find where to import it from? I am looking for these two warning: numpy\lib\function_base.py:2526: RuntimeWarning: Degrees of freedom <= 0 for slice c = cov(x, y, rowvar) \numpy\lib\function_base.py:2455: RuntimeWarning: divide by zero encountered in true_divide c *= np.true_divide(1, fact) – tim654321 Apr 19 '23 at 02:36
  • RuntimeWarning is a built-in so you don't need to import anything. – Kyle Apr 26 '23 at 03:46
  • But that will ignore all runtimewarnings, not just the two I am worried about – tim654321 Apr 28 '23 at 07:18
  • Ah then you will need to use `filterwarnings` and the `message` arg. – Kyle May 08 '23 at 15:22
9

The most flexible way is to combine warnings.filterwarnings() (or warnings.simplefilter()) with the warnings.catch_warnings() context manager. This way you ge the flexibility of filterwarnings but the filtering is only applied inside the with block:

import warnings

with warnings.catch_warnings():
    warnings.filterwarnings(
        action='ignore',
        category=SomeWarningClass,
        message='some message')
   
    # Do stuff that causes the warning

See The Warnings Filter in the docs for the descriptions of the arguments for filterwarnings.

Full example

Here is a full example with a custom warning:

import warnings

# Custom warning class for the example
class MyWarning(UserWarning):
    pass

def function_that_raises_warning(num):
    print(num)
    warnings.warn("This is a warning", MyWarning)


with warnings.catch_warnings():
    function_that_raises_warning(1) # raises warning
    warnings.filterwarnings(
        action="ignore", category=MyWarning, message="This is a warning"
    )

    function_that_raises_warning(2) # Warning is filtered
function_that_raises_warning(3) # raises warning

Example output

1
.\test_warnings.py:10: MyWarning: This is a warning
  warnings.warn("This is a warning", MyWarning)
2
3
.\test_warnings.py:10: MyWarning: This is a warning
  warnings.warn("This is a warning", MyWarning)
Niko Föhr
  • 28,336
  • 10
  • 93
  • 96
  • 1
    Hi @np8 for me it works using `warnings.simplefilter('ignore')` rather than `warnings.filterwarnings()` as per the current [docs](https://docs.python.org/3/library/warnings.html#temporarily-suppressing-warnings). – Mauricio Maroto May 31 '22 at 02:40
  • What happened when you tried `warnings.filterwarnings()`? – Niko Föhr May 31 '22 at 07:46