FAQ
Is there any way to access global variables defined from within an
imported module? For example, lets say have a file called test2.py
that defines a simple class:

class MyClass:
def __init__(self):
pass

def printGlobal(self):
print globalVar


Now I have another file that imports test2, sets a global variable
called globalVar, and calles printGlobal() like so:

from test2 import MyClass

globalVar = "foo"

mc = MyClass()
mc.printGlobal()


When I run it, I get a NameError that "global name 'globalVar' is not
defined". I've tried explicitly calling 'global globalVar' and even
stepping through the __builtins__ but to no avail. Is accessing
globalVar from within test2.py possible?

-Sean Levatino

Search Discussions

  • Christopher Koppler at Aug 4, 2003 at 4:14 pm

    On 4 Aug 2003 07:27:41 -0700, sean at activeprime.com (Sean) wrote:
    Is there any way to access global variables defined from within an
    imported module? For example, lets say have a file called test2.py
    that defines a simple class:

    class MyClass:
    def __init__(self):
    pass

    def printGlobal(self):
    print globalVar


    Now I have another file that imports test2, sets a global variable
    called globalVar, and calles printGlobal() like so:

    from test2 import MyClass

    globalVar = "foo"

    mc = MyClass()
    mc.printGlobal()


    When I run it, I get a NameError that "global name 'globalVar' is not
    defined". I've tried explicitly calling 'global globalVar' and even
    stepping through the __builtins__ but to no avail. Is accessing
    globalVar from within test2.py possible?

    Global variables are global at *module* level, which means that every
    class and function defined in that module can access that variable.
    Variables (and all other names) from *other* modules need to be
    imported in order to be used, thus the 'not defined'.

    In your case, however, you'd need to import the 'another file that
    imports test2' (I'll call it globalTest from here on) into test2, so
    you could access globalVar inside of test2; that would lead to a nasty
    case of circular import, and an ImportError exception will be thrown:

    Traceback (most recent call last):
    File "globalTest.py", line 1, in ?
    from test2 import MyClass
    File "C:\Python23\test2.py", line 1, in ?
    from globalTest import globalVar
    File "C:\Python23\globalTest.py", line 1, in ?
    from test2 import MyClass
    ImportError: cannot import name MyClass

    There might be a hack around that, but if there was, I wouldn't use
    it.



    To pass any value from globalTest to test2.MyClass, pass it at class
    instantiation - it becomes a variable local to the class instance.
    Directly adapting your example:

    #test2.py
    class MyClass:
    def __init__(self, val):
    self.value = val
    def printGlobal(self):
    print self.value

    #globalTest.py
    from test2 import MyClass

    globalVar = "foo"

    mc = MyClass(globalVar)
    mc.printGlobal()



    So far so good, this runs as it should.

    Now if you tell us what you really wanted to do, maybe we could give
    you a more helpful answer...


    --Christopher
  • John J. Lee at Aug 4, 2003 at 6:11 pm

    sean at activeprime.com (Sean) writes:

    Is there any way to access global variables defined from within an
    imported module? For example, lets say have a file called test2.py
    that defines a simple class: [...]
    Now I have another file that imports test2, sets a global variable
    called globalVar, and calles printGlobal() like so:
    [...]

    You could explicitly pass MyClass constructor the globals dict
    (globals()), and have it use that. Not very nice.

    What's your end goal with this, though?

    Why not have a class to represent this 'global' state (since it seems
    it isn't really global)?


    John
  • StasZ at Aug 4, 2003 at 6:24 pm

    On Mon, 04 Aug 2003 07:27:41 -0700, Sean wrote:

    Is there any way to access global variables defined from within an
    imported module? For example, lets say have a file called test2.py
    that defines a simple class:

    class MyClass:
    def __init__(self):
    pass

    def printGlobal(self):
    print globalVar


    Now I have another file that imports test2, sets a global variable
    called globalVar, and calles printGlobal() like so:

    from test2 import MyClass

    globalVar = "foo"

    mc = MyClass()
    mc.printGlobal()
    You can try overloading MyClass.

    from test2 import MyClass

    globalVar = "foo"

    class HisClass(MyClass):

    def printGlobal(self):
    print globalVar


    mc = HisClass()
    mc.printGlobal()

    Stas Z
  • Sean at Aug 5, 2003 at 2:39 pm
    Thanks for the help guys. Passing the variable as a class parameter
    (or even function parameter) is not really a possibility due to the
    structure of the actual program code. The real world problem is that
    I'm defining a global variable for a base application path for my
    program, which changes depending on whether the program is being run
    as a script, an compiled exe, and if there are preconfigured settings
    in the windows registry and/or a config file. This was previously
    being done in the global space of the "main" application script. The
    class that needed this info was about 5 or six steps down the import
    tree.

    I think my solution is going to involve taking all the logic for
    finding the base path out of the main app script and putting it in
    it's own module. That way anything that needs to see that variable
    can just import that module and not have to worry about strange import
    loops.

    -Sean Levatino
  • Peter Abel at Aug 5, 2003 at 7:58 pm
    sean at activeprime.com (Sean) wrote in message news:<ef7a350b.0308050639.6ffa33f0 at posting.google.com>...
    Thanks for the help guys. Passing the variable as a class parameter
    (or even function parameter) is not really a possibility due to the
    structure of the actual program code. The real world problem is that
    I'm defining a global variable for a base application path for my
    program, which changes depending on whether the program is being run
    as a script, an compiled exe, and if there are preconfigured settings
    in the windows registry and/or a config file. This was previously
    being done in the global space of the "main" application script. The
    class that needed this info was about 5 or six steps down the import
    tree.

    I think my solution is going to involve taking all the logic for
    finding the base path out of the main app script and putting it in
    it's own module. That way anything that needs to see that variable
    can just import that module and not have to worry about strange import
    loops.

    -Sean Levatino
    At my knowledge **global** is always global in the module's
    namespace. That means your **MyClass.printGlobal()** methode is
    searching for **globalVar** in the namespace of the module **test2**.
    When you say **from test2 import MyClass** then MyClass is an attribute
    in the actual namespace where you're working. But it's searching
    **globalVar** in the namespace of **test2**.
    So the following works for me:
    import test2
    mc = test2.MyClass()
    test2.globalVar='foo'
    mc.printGlobal()
    foo
    >>>
    To proove the above said even the following works:
    from test2 import MyClass
    import test2
    test2.globalVar='foo'
    ## !!ATTENTION!! The following is NOT **mc=test2.MyClass()**
    mc=MyClass()
    mc.printGlobal()
    foo
    test2.globalVar='Something else'
    mc.printGlobal()
    Something else
    >>>

    Regards
    Peter

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 4, '03 at 2:27p
activeAug 5, '03 at 7:58p
posts6
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase