By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,766 Members | 1,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,766 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

May 6 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
wrote in news:11**********************@e65g2000hsc.googlegr oups.com 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:
>>import exceptions
help( exceptions )
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/
May 6 '07 #2

P: n/a
On May 6, 11:23 am, Rob Williscroft <r...@freenet.co.ukwrote:
wrote innews:11**********************@e65g2000hsc.google groups.comin
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:
>import exceptions
help( exceptions )

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.

May 6 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.