How to suppress a third-party warning using warnings.filterwarnings

前端 未结 4 2041
忘了有多久
忘了有多久 2021-02-05 00:55

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         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 01:15

    The most flexible way is to combine warnings.filterwarnings() 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
    from Crypto.pct_warnings import RandomPool_DeprecationWarning
    
    with warnings.catch_warnings():
        warnings.filterwarnings(
            action='ignore',
            category=RandomPool_DeprecationWarning,
            message='This application uses RandomPool, which is BROKEN in older releases')
       
        # Do stuff that causes the warning
    
    

提交回复
热议问题