473,551 Members | 2,775 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Importing Module To Use Variables In A Second Module

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
Sep 27 '07 #1
4 2190
rs******@nospam .appl-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(variableP age)
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

Sep 27 '07 #2
On 2007-09-27, Steve Holden <st***@holdenwe b.comwrote:
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(variablePa ge)

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
Sep 28 '07 #3
rs******@nospam .appl-ecosys.com wrote:
On 2007-09-27, Steve Holden <st***@holdenwe b.comwrote:
>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(variabl ePage)
and you will see exactly what names the module is defining in its
namespace. You could also try
>>>>variablePag e.__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.in stancename.attr ibutename

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

Sep 28 '07 #4
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, rs******@nospam .appl-ecosys.com
<rs******@nospa m.appl-ecosys.comwrote :
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/
Sep 28 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2214
by: Jeff Wagner | last post by:
I am importing a file which contains a persons name (firstName, middleName, etc). If I define a function to do this, how can I use the variables outside of that function? Here is the code: import string def getName(): data = open("enterName.txt")
1
1777
by: J. Kenney | last post by:
Good Morning, Is there a way for a function called within an _imported_ library to call back to _calling_ python program to run a function? (Shown below) Also can you overload the print function to have it do redirect output to an array for a bit, and then switch it back. (for example: right before importing make print append each line...
12
2362
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In some cases there is a further complication: module importing through an indirect mechanism, like: exec "from " + xxx + " import *". A part the fact...
4
2654
by: jean-marc | last post by:
As an application programmer, I'm not well versed in the material aspects of computing (memory, cpu, bus and all). My understanding of imports in Python is such: the __main__ program is the center piece which holds the programs reference: globals, functions, classes, modules etc. The objects of this file (functions and classes) are directly...
9
2158
by: rbygscrsepda | last post by:
Hi, I'm a newbie at Python. :) Right now it's not letting me import * from any relative package name--i.e., a name that starts with a dot. For instance, none of the following work: from . import * from .sibiling import * from .. import * from ..parent_sibling import * ....and so on. The same error occurs:
5
1229
by: Matt_D | last post by:
Good afternoon. As a self-tutoring project I am writing a one-time-pad encrypt/decrypt script. I have completed the encryption portion and am working currently on the decryption algorithm. My goal is to have the encrypt and decrypt be individual modules vice two parts of the same. My problem, or perhaps more accurately, question, lies in...
3
1791
by: rs387 | last post by:
Hi, I've found the following behaviour on importing a variable from a module somewhat odd. The behaviour is identical in Python 2.5 and 3.0b2. In summary, here's what happens. I have a module, oddmodule.py (below), that defines a variable, OddVariable, by assigning a value A to it. The file I execute, mainfile.py, imports and re-binds...
2
2200
by: Robert Dailey | last post by:
Hi, I'm currently using boost::python::import() to import Python modules, so I'm not sure exactly which Python API function it is calling to import these files. I posted to the Boost.Python mailing list with this question and they said I'd probably get a better answer here, so here it goes... If I do the following:
0
7565
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7768
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7847
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6106
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5130
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3520
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1097
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.