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/SetConnections.py:148: DeprecationWarning: os.popen2 is deprecated. Use the subprocess module.
stdin, stdout = os.popen2(remote_cmd)

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

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

Of course, look for sitecustomize.py 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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

patch rdiff-backup

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.


@DPATCH@
diff -urNad rdiff-backup-1.2.8~/rdiff_backup/SetConnections.py rdiff-backup-1.2.8/rdiff_backup/SetConnections.py
--- rdiff-backup-1.2.8~/rdiff_backup/SetConnections.py 2009-03-16 15:36:21.000000000 +0100
+++ rdiff-backup-1.2.8/rdiff_backup/SetConnections.py 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 os.name == "nt":
+ if map(int, sys.version.split()[0].split('.')[:2]) >= [2, 6]:
import subprocess
try:
- process = subprocess.Popen(remote_cmd, shell=False, bufsize=0,
+ process = subprocess.Popen(remote_cmd, shell=True, bufsize=0,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
(stdin, stdout) = (process.stdin, process.stdout)