Refactor the Python 2.5 compatibility code in the 'except ImportError' block