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

2010-02-10 Thread Kent Johnson
On Tue, Feb 9, 2010 at 10:00 PM, David ld...@gmx.net wrote:
 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!?

Python caches modules (in sys.modules). When you run inside of
ipython, you are not restarting the interpreter so you don't get a
clean module cache. This has implications for multi-module
development.

Suppose you have

# module.py
animls = ['cat', 'dog' ] # note the misspelling

# main.py
import module
print module.animals

If you run main.py you will get a NameError and the erroneous
module.py will be cached in the module cache. If you correct module.py
and again run main.py you will get the same error because module.py
has not been reloaded.

A couple of work-arounds-
- run main.py from a command line or some other way that gives it a
fresh interpreter
- from ipython command line, or in main.py
  import module
  reload(module)

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-02-10 Thread Dave Angel

David wrote:
div class=moz-text-flowed style=font-family: -moz-fixedHello 
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 tocelsius2=
and after running and getting the error, change the following line to 
call celsius2().  If it gets an error, notice what symbol it complains 
about.


HTH
DaveA




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-02-09 Thread David

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?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-02-09 Thread wesley chun
 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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2007,2001
Python Fundamentals, Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-02-09 Thread David

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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2007,2001
Python Fundamentals, Prentice Hall, (c)2009
 http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor