Re: ActiveState Python won't call module function

From: John Benson (jsbenson_at_bensonsystems.com)
Date: 11/29/03


Date: Sat, 29 Nov 2003 13:39:11 -0800
To: <python-list@python.org>


"import" in the following makes x but not y a global, so y must be qualified
in the importing module:

import module x
...
    z = x.y()

where x is the imported module, y is a class in module x, and z is the
reference to the new instance of y

In your case,

thisClass = module1.module2()

should solve the problem.

Another way to do it is using the "from" statement which adds the module's
globals to your globals:

from x import y
...
    z = y()

or, worse,

from x import *
...
    z = y()

but the documentation cautions against wholesale importation of module names
into the caller's namespace unless the imported module has been designed
specifically for it (e.g. Tkinter). Purists rebel against even this usage of
"from ... import *"



Relevant Pages

  • Re: Why I need to declare import as global in function
    ... >> I have remarq that this problem is raised when I execute code in an ... >> imported module ... The execfile target seems to have been ... so adding some globals to the execfile call ...
    (comp.lang.python)
  • Global var access in imported modules?
    ... Then I also proceed to initiallize some global variables ... a value to my variable sName. ... But also, what if I need to access "sName" in another imported module, say "otherstuff.py"? ... Is there some way I can define globals in my main module, and have them accessible in all my imported submodule? ...
    (comp.lang.python)