473,320 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
2 5269
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Xah Lee | last post by:
Python doc problem: http://python.org/doc/2.4.2/lib/os-file-dir.html makedirs( path) Recursive directory creation function. Like mkdir(), but makes all intermediate-level directories needed...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
6
by: Steve Amey | last post by:
Hi all I want to be able to throw a custom error up the call stack. I have looked around and it seems as though it's possible, but I can't get it to work :o( Below is some sample code. ...
6
by: aomighty | last post by:
I wanted to write the following code: import shelve try: db = shelve.open(file, "r") except SomeError: print "Oh no, db not found" Only, I'm not sure what SomeError should be. I tried...
5
by: Jeff | last post by:
Okay, I'm still new to vb.net 2005 - throught this was a hardware problem, but now I don't know. (I'm having some problem with my newgroup provider, so hopefully this will go through) This...
2
by: Gabriel Genellina | last post by:
En Sun, 15 Jun 2008 05:35:18 -0300, Maryam Saeedi <ma.saeedi@gmail.comescribió: NoneType is not an exception, but the type of the None object. Perhaps you're not interpreting correctly some...
4
by: Cirene | last post by:
In my web.config I added <pages enableSessionState="true">. In each of my pages I also added EnableSessionState="True" to the Page declaration. (I didn't think this was necessary, but...) ...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.