[Tutor] NameError: global name 'celsius' is not defined (actually, solved)

David ldl08 at gmx.net
Wed Feb 10 04:00:05 CET 2010


Hi guys,

I just wrote this message, but after restarting ipython all worked fine.
How is it to be explained that I first had a namespace error which, 
after a restart (and not merely a new "run Sande_celsius-main.py"), went 
away? I mean, surely the namespace should not be impacted by ipython at 
all!?

Many thanks,

David


Dear List,

this should be so basic that I feel bad asking this question here, but I 
don't get it.

I am having a look at Sande's book "Hello World". The topic is 
'Modules', and the code comes directly from the book.

I have two files: Sande_celsius-main.py and Sande_my_module.py.

I import the latter from within the former.

# file: Sande_celsius-main.py
from Sande_my_module import c_to_f
celsius = float(raw_input("Enter a temperature in Celsius: "))
fahrenheit = c_to_f(celsius)
print "That's ", fahrenheit, " degrees Fahrenheit"


# this is the file Sande_my_module.py
# we're going to use it in another program
def c_to_f(celsius):
     fahrenheit = celsius * 9.0 / 5 + 32
     return fahrenheit

When I run Sande_celsius-main.py, I get the following error:

NameError: global name 'celsius' is not defined
WARNING: Failure executing file: <Sande_celsius-main.py>

First of all, this error message doesn't exactly tell me _where_ the 
problem is, does it? It could be a problem with(in) the imported 
function c_to_f... I wish he would tell me: "Problem in file x, line y".

Secondly, the name celsius in the global namespace of ~-main.py is 
merely a variable, which later is then used as a parameter to c_to_f. I 
do not see a problem here.

What is going on?


More information about the Tutor mailing list