2

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
4
  • 3
    Python 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. Commented Aug 23, 2012 at 15:05
  • 1
    Please read PEP8: python.org/dev/peps/pep-0008 Commented Aug 23, 2012 at 15:15
  • 4
    Thanks 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) Commented 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 useful Commented Dec 20, 2016 at 7:08

1 Answer 1

10

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.