I just “thought”, I understood how the modules are imported and work, but obviously I need to learn more.
Here is an example program (just a test example of what I am doing, it is much larger in scale and scale) and a module:
quick.py
import gtk from quick_window import * w.show_all() gtk.main()
quick_window.py
w = gtk.Window() w.connect('destroy', lambda w: gtk.main_quit()) l=gtk.Label('Hello') w.add(l)
running I get
$ python quick.py Traceback (most recent call last): File "quick.py", line 2, in <module> from quick_window import * File "/home/woodnt/Dropbox/python/weather_project/plugins/quick_window.py", line 3, in <module> w = gtk.Window() NameError: name 'gtk' is not defined
To make it work, I also need to import (er, reimport) gtk in the module like this:
import gtk w = gtk.Window() w.connect('destroy', lambda w: gtk.main_quit()) l=gtk.Label('Hello') w.add(l)
Why do I need to import gtk more than once? Does this mean that I have 2 "gtk" in memory?
Do I need to import everything inside each module that I need in this module?
I know that each module has its own namespace, but I thought that it also inherited "globals", including the imported module from the calling program.
I had the impression that from the import * module it is like cutting and pasting code directly into this place. Is there any other way to do this?
Help is appreciated.
Narnie
python import module
narnie
source share