parsed.org

Tips by tag: psycopg2

Psycopg1 Compatibility Mode by cygnus on Dec 31, 2005 04:07 PM

If you're using the psycopg Python module to connect to Postgres, you may find that you have old scripts that use version 1 of psycopg but you have version 2 installed and don't want to modify your scripts. At the time of this writing (and according to http://initd.org/tracker/psycopg/wiki/Migration), the version 2 module provides a very easy "compatibility mode". Just import the psycopg1 submodule and alias it, or fall back to the version 1 module if version 2 is not available:

try:
    # Try importing the compatibility submodule, which will only
    # work if psycopg version 2 is available.
    import psycopg.psycopg1 as psycopg
except Exception, e:
    # Fall back to version 1.
    import psycopg
aliasdbapiimportlanguagespostgresqlprogrammingpsycopgpsycopg2python
RSS