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

Dave Angel davea at ieee.org
Wed Feb 10 15:00:29 CET 2010

David wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Hello 
> Wesley,
> thanks for your reply. I was surprised about the limited information 
> too. Sadly (?), I can't reproduce the error any more...
> David
> On 10/02/10 11:13, wesley chun wrote:
>>> 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!?
>>>          :
>>> # 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>
>> Python interpreters including the standard one or IPython should tell
>> you a lot more than that. how are you executing this code? would it be
>> possible to do so from the command-line? you should get a more verbose
>> error message that you can post here.
>> best regards,
>> -- wesley
Your response to Wesley should have been here, instead of at the top.  
Please don't top-post on this forum.

I don't use iPython, so this is just a guess.  But perhaps the problem 
is that once you've imported the code, then change it, it's trying to 
run the old code instead of the changed code.

Try an experiment in your environment.  Deliberately add an error to 
Sande_celsius-main.py, and run it.  Then correct it, and run it again, 
to see if it notices the fix.

The changes I'd try in this experiment are to first change the name on 
the celsius= line to    celsius2=
and after running and getting the error, change the following line to 
call celsius2().  If it gets an error, notice what symbol it complains 


More information about the Tutor mailing list