473,785 Members | 2,969 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 2200
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
2235
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
1784
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 to an array vice printing it, then going
12
2406
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 that I have not understood the "real" difference between import and from ... import (or also from......
4
2668
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 accessible; 'import suchModule' s objects are attainable through the *qualified name*...
9
2172
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
1238
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 importing a function from the otp_encrypt script. Here is the function I am attempting to call:
3
1803
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 OddVariable to a value B. I have two test modules which import the
2
2207
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
9645
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8972
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.