Aquarionics

Tuesday 11th May 2004

Python annoyance

Given the following code:

def index():
        try:
                import MySQLdb
        except:
                return "Couldn't import MySQL module"
        return "Yay Workyness"

inside a mod_python instance, is there any reason why it should return “Yay Workyness” and “Couldn’t import MySQL module” in roughly equal amounts? (MySQLdb in this case is a sub directory of the current working directory). Bugs I can cope with, but this inconsistancy is driving me insane…

Those who spoke on this:

gravatar image

Simon Willison:

2004-05-11 17:57 3 hrs after the Original Article

I good rule of thumb is that naked excepts are eeeevil and should be avoided at all costs. The problem is that you don’t know if you’re catching an ImportError or an error deeper down, somewhere in the MySQLdb module itself. Change your except line to “except ImportError” and see if you get a different exception rising to the top.

Comment Link

gravatar image

Aquarion:

2004-05-11 18:22 25 mins after Simon Willison

That makes sense. Still don’t know what’s going on, but asking my Friendly Neighbourhood Sysadmin to install the module globally appears to have made it Go Away

Comment Link

gravatar image

Rory Parle:

2004-05-11 22:40 7 hrs after the Original Article

I like your header graphic. It seems Mr Adams is spending rather more than a year dead for tax reasons. Unfortunately the world is doing a woeful job of finding a replacement.

Comment Link


Nicholas 'Aquarion' Avenell is a web developer in London, you can find out more about him or how to get in touch.

There are more Articles, Projects, Journal Entries, Photographs and things that defy description here, too.

If you're looking for something specific, there are Calendar & Category -based lists of everything.

And if you want to follow stuff that appears here, try a Syndication Feed, or the generic Feed of everything.

Aquarionics on Livejournal


Aquarion [updating]
Twitter last updated


More Journal:

[RSS Icon]
[ESF Icon]
[CDF Icon]

That which is relevant:


Explain Ads
© 2000 to 2008 inclusive Nicholas Avenell
All comments are the property of their creators, published with permission
(Unless otherwise indicated, the opinions and sentiments expressed on this site are those of the author and not of any organisation of which he is an affiliate, including his employer. Caveat Lector, E&OE. sigh)
0.332 seconds, 22 queries, 2.66Mb on Mon, 10 Nov 2008 20:31:06 +0000
Generated by Epistula Version 2.0.3