Easiest way would be as the warnings module suggests here:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import paramiko
Answer from snapshoe on Stack OverflowEasiest way would be as the warnings module suggests here:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import paramiko
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
Look at the Temporarily Suppressing 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_warningscontext manager:import warnings def fxn(): warnings.warn("deprecated", DeprecationWarning) with warnings.catch_warnings(): warnings.simplefilter("ignore") fxn() # Or if you are using > Python 3.11: with warnings.catch_warnings(action="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
<stdin>:3: UserWarning: you are warned!
after
>>> warnings.filterwarnings("ignore")
>>> f()
before
after
There's the -W option.
python -W ignore foo.py