I have an exception class defined
#####UNIQUE CONSTRAINT EXCEPTION#########################################################3
class UniqueConstraintException (Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr('Failed unique property. Property name: ' + self.value)
The file name is: "UniqueConstraintException.py" and package name: "exception"
I'm importing and using it in this way:
from exception import UniqueConstraintException
raise UniqueConstraintException(prop_key)
And get this error:
TypeError: 'module' object is not callable
What am i doing wrong?
asked Aug 23, 2012 at 15:00
Jaime Rivera
1,4265 gold badges18 silver badges31 bronze badges
-
3Python is not Java, that's what's wrong. You don't go around defining 4 line modules just to have a single class per file. Put your exceptions together. And for that matter, think long and hard before adding a new exception type. There's nothing wrong with the built-in ones.user395760– user3957602012年08月23日 15:05:10 +00:00Commented Aug 23, 2012 at 15:05
-
1Please read PEP8: python.org/dev/peps/pep-0008bukzor– bukzor2012年08月23日 15:15:53 +00:00Commented Aug 23, 2012 at 15:15
-
4Thanks for being a phallus about it, delnan. A lot of passive aggression in there. You know, some of us don't know everything. If only we could be as knowledgeable as you. (P.S. For an additional example of passive aggression please re-read this comment)John Carrell– John Carrell2016年02月18日 20:35:29 +00:00Commented Feb 18, 2016 at 20:35
-
i just wanted to comment that despite delnan's answer coming across as pretty aggressive, coming from Java, learning python and reading the blogpost that he posted after a couple weeks of study was pretty helpful in rethinking how i approach python. defining your own exceptions is rarely usefulfrei– frei2016年12月20日 07:08:01 +00:00Commented Dec 20, 2016 at 7:08
1 Answer 1
This is why you want to keep your module names lower-cased. :-)
from exception.UniqueConstraintException import UniqueConstraintException
You imported the module, no the class defined inside of the module.
answered Aug 23, 2012 at 15:01
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Martijn Pieters
In this case? Just put all your exceptions in one
exceptions.py module. In the general case, I'd name the module uniqueconstraint.py perhaps.lang-py