Skip to content
Apr 19 / Nizam Sayeed

Suppressing MySQL/MySQLdb warning messages from Python

Here is a quick three line snippet that will suppress all of those annoying warning messages from MySQL when using MySQLdb:

from warnings import filterwarnings
import MySQLdb as Database
filterwarnings('ignore', category = Database.Warning)

To re-enable the warnings later on:

from warnings import resetwarnings

More about the warnings Python standard library:

  • Drew

    Thanks.That was useful. Was tired of seeing
    “Statement is not safe to log…” filling up my logs.

  • Joel Madigan

    Very cool! I had no idea that module even existed. Thanks!

  • online zeitung

    great thx.

    can you help with IOError: failed to write data
    seems to be from mod_wsgi (pid=20083): Exception occurred processing WSGI script ‘/var/www/’

  • Melodic Method

    That is excellent! You rock.