namespace query

Peter Otten __peter__ at web.de
Wed Apr 22 08:34:11 EDT 2009


Dr Mephesto wrote:

> I have a quick question about global namespace, and I'm hoping someone
> could give a quick reply to sort me out :)
> 
> I have a single file program that uses numpy, and it works just fine.
> I want to move some classes into their own files, to make the code
> reusable.  When I cut and paste the the classes into new files, and
> then "import" them back into the original program, I get an
> error,:"NameError: name 'numpy' is not defined", called about the new
> class files.
> 
> If I add a "global numpy" to the beginning of each class in the new
> files, the program runs. Do I really have to add "global XXX" for
> every module I import in the main program into every module I create
> and import? Why are the class files I created not seeing the top
> namespace?

The global namespace in Python is actually a module-wide namespace. You have
to put an

import numpy

at the top of every module that uses numpy. 

I lack the fantasy to imagine what exactly you have done that "global numpy"
has the described effect. It's probably not pretty.

Peter



More information about the Python-list mailing list