Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Importing modules

Reply
Thread Tools

Importing modules

 
 
Fredrik Lundh
Guest
Posts: n/a
 
      05-12-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Another item (for me...) difficult, is "import modules", and "plenty of
> information" (as you said) does not help me much: the mechanism of
> variable visibility and namespaces is not clear to me.


have you read this

http://effbot.org/zone/python-objects.htm

and

http://effbot.org/zone/import-confusion.htm

?

</F>



 
Reply With Quote
 
 
 
 
qwweeeit@yahoo.it
Guest
Posts: n/a
 
      05-13-2005
Hi Fredrik,
thank you very much for your articles (especially that on
import-confusion). At last I grasped, also if not thorougly, the import
mechanism. What cleared my doubts was your recursive import paragraph.
In the Guido's "Python Reference Manual" he says:
"Import statements are executed in two steps: (1) find a module , and
initialize it if necessary..."
....almost like it was not necessary to go through it executing it (and
initializing the classes if necessary).
I made a small script to be sure and (...of course) confirmed what you
said...
I have still some doubts on the namespaces and functions/variables
visibility, but for that there is the error mechanism...
Bye.

 
Reply With Quote
 
 
 
 
jmdeschamps@cvm.qc.ca
Guest
Posts: n/a
 
      05-14-2005
CONTEXT:
Trying to modularize (is this english?) application development : MVC
style, team distributed so that a visual component can be in one module
imported and used by the __main__, and connected (read packed) at
runtime to other components coming from the main or other modules.

PROBLEM:
After reading the above reference (and the Nutshell chapter) and trying
different importing variations I still have a very difficult time
understanding this situation.
1-importing Tkinter in main,
2-importing module that uses Tkinter, without importing Tkinter in that
module (DOES NOT WORK)
KLUDGED SOLUTION:
import Tkinter in module (but don't understand why it has to be this
way)

MODEL (I taught would work):
## importTestMain module
from Tkinter import *
import testModule

tata = testModule.toto() ## this works
myWidget = tata.createNewFrame() ## this gets the exception (see below)
....

## testModule
class toto(object):
def __init__(self):
self.someAttribute= 2
def createNewFrame(self):
self.myFrame = Frame() ## culprit line in exception
return f

if __main__=='__name__': ## team member tests is work here (does
work!)
from Tkinter import *
myInstance = otherModule.toto()
myWidget = myInstance.createNewFrame()
root=Tk()
myWidget.pack(in_=root)
root.mainloop()

The exception says :
Traceback (most recent call last):
File
"C:\Python23\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py" ,
line 310, in RunScript
exec codeObject in __main__.__dict__
File
"C:\jean_marc\exemples_code_source\modules_package s\import_test\importTestMain.py",
line 18, in ?
tata=toto()
File
"C:\jean_marc\exemples_code_source\modules_package s\import_test\importTestMain.py",
line 10, in __init__
self.myNewFrame =
self.myTestModuleObject.createNewFrame(self.myInte rModuleTestCommand)
File
"C:\jean_marc\exemples_code_source\modules_package s\import_test\testModule.py",
line 8, in createNewFrame
self.myFrame = Frame()
NameError: global name 'Frame' is not defined
>>>


VARIATION
Imported testModule AFTER using Tkinter in main... same error

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing v reloading modules modules Peter Peyman Puk Python 0 03-19-2010 05:09 PM
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM
Importing modules from within other modules Tobiah Python 2 09-14-2003 09:18 PM
Importing/reloading modules OKB (not okblacke) Python 0 09-04-2003 05:25 AM



Advertisments