Unable to suppress deprecation warnings

前端 未结 1 768
攒了一身酷
攒了一身酷 2021-01-03 04:26

In my Django application, when I import one third party library, I get this warning in the console:

the imp module is deprecated in favour of importli

相关标签:
1条回答
  • 2021-01-03 04:48

    There are a couple of issues with the code you've tried. If you want to filter PendingDeprecationWarning, then you should use PendingDeprecationWarning in your code. Your code is using DeprecationWarning and RemovedInDjango110Warning, which are different warnings. Secondly, the fxn() function in the docs is an example function that creates a warning. It doesn't make sense to include it in your code.

    You can either filter all pending deprecation warnings

    import warnings
    warnings.simplefilter("ignore", category=PendingDeprecationWarning)
    

    However this might hide pending deprecations in your own code that you should fix. A better approach would be to use a context manager, to filter out warnings when importing the third-party lib.

    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=PendingDeprecationWarning)
        from third_party_lib import some_module
    
    0 讨论(0)
提交回复
热议问题