Importing Module To Use Variables In A Second Module

Discussion in 'Python' started by rshepard@nospam.appl-ecosys.com, Sep 27, 2007.

  1. -ecosys.com

    -ecosys.com Guest

    I'm stymied by what should be a simple Python task: accessing the value of
    a variable assigned in one module from within a second module. I wonder if
    someone here can help clarify my thinking. I've re-read Chapter 16 (Module
    Basics) in Lutz and Ascher's "Learning Python" but it's not working for me.

    In one module (the "source"), variablePage.py, three wxPython widgets
    display values that are assigned to variables: curVar, UoDlow, and UoDhigh.
    I want to display then in equivalent widgets on a wxPython notebook tab in a
    different module, the "importer."

    At the top of the importer module I have:

    from variablePage import curVar, UoDlow, UoDhigh

    and I try to display the values of those variables in widgets on this page.
    But, python complains:

    from variablePage import curVar, UoDlow, UoDhigh
    ImportError: cannot import name curVar

    I've also tried

    import variablePage as VP

    and referenced the variables as VP.curVar, VP.UoDlow, and VP.UoDhigh, but
    python still doesn't like this:

    File "/data1/eikos/fuzSetPage.py", line 364, in loadParVar
    first = VP.curVar
    AttributeError: 'module' object has no attribute 'curVar'

    Both of these forms are used in the book (pages 260-261) in simple
    examples. I also get errors if I try importing using the class name before
    the variable name.

    A clue stick would be very helpful since I am not seeing just what I'm
    doing incorrectly.

    Rich
    -ecosys.com, Sep 27, 2007
    #1
    1. Advertising

  2. -ecosys.com

    Steve Holden Guest

    -ecosys.com wrote:
    > I'm stymied by what should be a simple Python task: accessing the value of
    > a variable assigned in one module from within a second module. I wonder if
    > someone here can help clarify my thinking. I've re-read Chapter 16 (Module
    > Basics) in Lutz and Ascher's "Learning Python" but it's not working for me.
    >
    > In one module (the "source"), variablePage.py, three wxPython widgets
    > display values that are assigned to variables: curVar, UoDlow, and UoDhigh.
    > I want to display then in equivalent widgets on a wxPython notebook tab in a
    > different module, the "importer."
    >
    > At the top of the importer module I have:
    >
    > from variablePage import curVar, UoDlow, UoDhigh
    >
    > and I try to display the values of those variables in widgets on this page.
    > But, python complains:
    >
    > from variablePage import curVar, UoDlow, UoDhigh
    > ImportError: cannot import name curVar
    >
    > I've also tried
    >
    > import variablePage as VP
    >
    > and referenced the variables as VP.curVar, VP.UoDlow, and VP.UoDhigh, but
    > python still doesn't like this:
    >
    > File "/data1/eikos/fuzSetPage.py", line 364, in loadParVar
    > first = VP.curVar
    > AttributeError: 'module' object has no attribute 'curVar'
    >
    > Both of these forms are used in the book (pages 260-261) in simple
    > examples. I also get errors if I try importing using the class name before
    > the variable name.
    >
    > A clue stick would be very helpful since I am not seeing just what I'm
    > doing incorrectly.
    >
    > Rich


    Self-evidently you are *not* creating the variables you think you are in
    the variablePage module. Have you tried an interactive test? Try this at
    the interpreter prompt:

    >>> import variablePage
    >>> dir(variablePage)


    and you will see exactly what names the module is defining in its
    namespace. You could also try

    >>> variablePage.__file__


    to make sure that you are importing the module you think you are.

    Otherwise, do you have circular imports? In other words, does the
    variablePage module by any chance import the module that's trying to
    import *it*? That's also a fruitful source of errors.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden

    Sorry, the dog ate my .sigline
    Steve Holden, Sep 27, 2007
    #2
    1. Advertising

  3. -ecosys.com

    -ecosys.com Guest

    On 2007-09-27, Steve Holden <> wrote:

    > Self-evidently you are *not* creating the variables you think you are in
    > the variablePage module. Have you tried an interactive test? Try this at
    > the interpreter prompt:
    >
    > >>> import variablePage
    > >>> dir(variablePage)

    >
    > and you will see exactly what names the module is defining in its
    > namespace. You could also try
    >
    > >>> variablePage.__file__

    >
    > to make sure that you are importing the module you think you are.


    Steve,

    Many thanks.

    I _think_ the problem is that the wxPython objects are not instatiated
    when I'm trying to access the variables. Since they are instance variables,
    they just don't exist yet.

    Learning just when wxPython objects are instantiated is something I've
    just begun to do.

    Rich
    -ecosys.com, Sep 28, 2007
    #3
  4. -ecosys.com

    Steve Holden Guest

    -ecosys.com wrote:
    > On 2007-09-27, Steve Holden <> wrote:
    >
    >> Self-evidently you are *not* creating the variables you think you are in
    >> the variablePage module. Have you tried an interactive test? Try this at
    >> the interpreter prompt:
    >>
    >>>>> import variablePage
    >>>>> dir(variablePage)

    >> and you will see exactly what names the module is defining in its
    >> namespace. You could also try
    >>
    >>>>> variablePage.__file__

    >> to make sure that you are importing the module you think you are.

    >
    > Steve,
    >
    > Many thanks.
    >
    > I _think_ the problem is that the wxPython objects are not instatiated
    > when I'm trying to access the variables. Since they are instance variables,
    > they just don't exist yet.
    >
    > Learning just when wxPython objects are instantiated is something I've
    > just begun to do.
    >
    > Rich


    That could be. If they are instance variables, however, you will find
    you have to access them as

    variablePage.instancename.attributename

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden

    Sorry, the dog ate my .sigline
    Steve Holden, Sep 28, 2007
    #4
  5. Most problems like this are caused by trying to access the attributes
    of the module before that module is fully executed, and thus before
    they are defined.

    For example, if you have A.py:
    import B
    data = "TEST"

    And B.py:
    import A
    print A.data

    Now, if you run A,py, it will import B before it defines its global
    variable 'data', so that when the B module tries to import A and then
    print A.data, it has not been defined yet. Look into the possibility
    that such an import loop is occurring in your code. You can import in
    loops, but not if you access things defined globally in one from a
    global scope in another.

    On 27 Sep 2007 21:30:10 GMT, -ecosys.com
    <-ecosys.com> wrote:
    > I'm stymied by what should be a simple Python task: accessing the value of
    > a variable assigned in one module from within a second module. I wonder if
    > someone here can help clarify my thinking. I've re-read Chapter 16 (Module
    > Basics) in Lutz and Ascher's "Learning Python" but it's not working for me.
    >
    > In one module (the "source"), variablePage.py, three wxPython widgets
    > display values that are assigned to variables: curVar, UoDlow, and UoDhigh.
    > I want to display then in equivalent widgets on a wxPython notebook tab in a
    > different module, the "importer."
    >
    > At the top of the importer module I have:
    >
    > from variablePage import curVar, UoDlow, UoDhigh
    >
    > and I try to display the values of those variables in widgets on this page.
    > But, python complains:
    >
    > from variablePage import curVar, UoDlow, UoDhigh
    > ImportError: cannot import name curVar
    >
    > I've also tried
    >
    > import variablePage as VP
    >
    > and referenced the variables as VP.curVar, VP.UoDlow, and VP.UoDhigh, but
    > python still doesn't like this:
    >
    > File "/data1/eikos/fuzSetPage.py", line 364, in loadParVar
    > first = VP.curVar
    > AttributeError: 'module' object has no attribute 'curVar'
    >
    > Both of these forms are used in the book (pages 260-261) in simple
    > examples. I also get errors if I try importing using the class name before
    > the variable name.
    >
    > A clue stick would be very helpful since I am not seeing just what I'm
    > doing incorrectly.
    >
    > Rich
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    --
    Read my blog! I depend on your acceptance of my opinion! I am interesting!
    http://ironfroggy-code.blogspot.com/
    Calvin Spealman, Sep 28, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Wagner
    Replies:
    11
    Views:
    551
    Francis Avila
    Nov 30, 2003
  2. plb
    Replies:
    2
    Views:
    340
  3. Joseph Turian
    Replies:
    2
    Views:
    281
    Joseph Turian
    Mar 21, 2006
  4. Nikita Petrov
    Replies:
    2
    Views:
    104
    Gary Wright
    Apr 6, 2008
  5. yelipolok
    Replies:
    4
    Views:
    243
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page