How to ignore deprecation warnings in Python How to ignore deprecation warnings in Python python python

How to ignore deprecation warnings in Python


You should just fix your code but just in case,

import warningswarnings.filterwarnings("ignore", category=DeprecationWarning) 


I had these:

/home/eddyp/virtualenv/lib/python2.6/site-packages/Twisted-8.2.0-py2.6-linux-x86_64.egg/twisted/persisted/sob.py:12:DeprecationWarning: the md5 module is deprecated; use hashlib instead import os, md5, sys/home/eddyp/virtualenv/lib/python2.6/site-packages/Twisted-8.2.0-py2.6-linux-x86_64.egg/twisted/python/filepath.py:12:DeprecationWarning: the sha module is deprecated; use the hashlib module instead import sha

Fixed it with:

import warningswith warnings.catch_warnings():    warnings.filterwarnings("ignore",category=DeprecationWarning)    import md5, shayourcode()

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

import md5, sha


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