Error when using Custom Exception defined in a different python module.

P

pillappa

Hi,

I am hitting this error consistently and don't know why it's
happening. I would like to define all exceptions for my project in one
file and use them across the project. Here's a sample -

exceptions.py -

class MyException(StandardError):
def __init__(self, error):
self.myerror = error

tests.py -

from exceptions import *

class Test:
def __init__(self,filename):
if filename == "":
raise MyException("Raise custom error")

if __name__ == "__main__" :
test = Test("")


When the above is run, I get the following error -
NameError: global name 'MyException' is not defined

Thanks
loganwol
 
R

Rob Williscroft

wrote in in
comp.lang.python:
Hi,

I am hitting this error consistently and don't know why it's
happening. I would like to define all exceptions for my project in one
file and use them across the project. Here's a sample -

exceptions.py -

from exceptions import *
raise MyException("Raise custom error")

When the above is run, I get the following error -
NameError: global name 'MyException' is not defined


When you get this kind of error, goto a python prompt
(type python at a command prompt, or click on IDLE)
and try this:

I got this response (clipped):

Help on built-in module exceptions:

NAME
exceptions - Python's standard exception class hierarchy.

Another common module name to avoid is "test".

Rob.
 
P

pillappa

wrote incomp.lang.python:





When you get this kind of error, goto a python prompt
(type python at a command prompt, or click on IDLE)
and try this:


I got this response (clipped):

Help on built-in module exceptions:

NAME
exceptions - Python's standard exception class hierarchy.

Another common module name to avoid is "test".

Rob.
--http://www.victim-prime.dsl.pipex.com/

Doh!

Thanks for correcting my error Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top