How to shut up Python deprecation warnings

I don't know why Python applications are so verbose with deprecation warnings. Either the apps should be fixed, or the system should be configured to not emit those pesky warnings in production. Since the former is not happening, I found a way to do the latter. So, if you are sick and tired of warnings like this:

/usr/lib/pymodules/python2.6/rdiff_backup/ DeprecationWarning: os.popen2 is deprecated. Use the subprocess module.
stdin, stdout = os.popen2(remote_cmd)

edit your /etc/python2.6/ file and append these two lines at the very end of the file:

import warnings
warnings.simplefilter("ignore", DeprecationWarning)

Of course, look for elsewhere if it's not in the same directory as on my system (check /etc/pythonX.Y, /usr/lib/pythonX.Y and /usr/lib/pythonX.Y/site-packages directories). The correct location obviously depends on the Python version.



I just ran into that exact problem with rdiff-backup on Debian Squeeze, so I downloaded the source using `apt-get source rdiff-backup`, grabbed the patch file from the Ubuntu repo, and hand-patched the Debian source (leaving out all the Ubuntu specific comments, etc). I then ran dpkg-buildpackage and created my own rdiff-backup .deb file and installed it over the current one.

Works like a charm! No more python warnings, as this implements the change recommended by the deprecation message.

If I thought it'd do any good, I'd send the patch to the maintainer for the Debian package.

The patch file that does the code change looks like this:

#! /bin/sh /usr/share/dpatch/dpatch-run
## 02_python_2.6_deprecationwarning.dpatch by
## All lines beginning with `## DP:' are a description of the patch.
## DP: No description.

diff -urNad rdiff-backup-1.2.8~/rdiff_backup/ rdiff-backup-1.2.8/rdiff_backup/
--- rdiff-backup-1.2.8~/rdiff_backup/ 2009-03-16 15:36:21.000000000 +0100
+++ rdiff-backup-1.2.8/rdiff_backup/ 2009-10-03 19:27:54.935647306 +0200
@@ -135,10 +135,10 @@
if not remote_cmd: return Globals.local_connection

Log("Executing " + remote_cmd, 4)
- if == "nt":
+ if map(int, sys.version.split()[0].split('.')[:2]) >= [2, 6]:
import subprocess
- process = subprocess.Popen(remote_cmd, shell=False, bufsize=0,
+ process = subprocess.Popen(remote_cmd, shell=True, bufsize=0,
(stdin, stdout) = (process.stdin, process.stdout)