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

Kent Johnson kent37 at tds.net
Wed Feb 10 13:34:26 CET 2010


On Tue, Feb 9, 2010 at 10:00 PM, David <ldl08 at 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


More information about the Tutor mailing list