[Tutor] Running multiple version of Python on 1 windows machine

wesley chun wescpy at gmail.com
Wed Nov 1 09:44:34 CET 2006


> try
>    import sqlite3 as sqlite # Python 2.5
> except ImportError:
>    from pysqlite2 import dbapi2 as sqlite


i second kent's suggestion here.  in fact, i ran into a very similar
issue while working on the database chapter for the new edition of the
book.  i had 2.4.2 with pysqlite 2.1.3 on it, and then when the first
release of 2.5 came out, discovered sqlite3.  for testing and
compatibility reasons, i wanted the example app to run under both, so
i added practically the same lines to the code for ushuffle_db.py:

    if db == 'sqlite':
        try:
            import sqlite3
        except ImportError, e:
            try:
                from pysqlite2 import dbapi2 as sqlite3
            except ImportError, e:
                return None

        DB_EXC = sqlite3
            :

i save off the same module again as DB_EXC to catch exceptions -- this
value is different for different databases, i.e. "import
_mysql_exceptions as DB_EXC" or "from gadfly import gadfly; DB_EXC =
gadfly". then in other parts of the code, i can do stuff like:

    try:
        cur.execute('SELECT * FROM table')
    except DB_EXC.OperationalError, e:
        # handle error for any database type

the entire source can be found here if you're interested:
http://corepython.com -> Source Code -> ch21 -> ushuffle_db.py

good luck!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list