views:

63

answers:

2

Hello,

I just "thought" I understood how importing and modules work but obviously I need more schooling.

Here is an example program (just a test case of somerthing I'm doing that is much bigger in scope 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 get it to work, I have to also import (er, reimport) gtk in the module like so:

import gtk

w = gtk.Window()
w.connect('destroy', lambda w: gtk.main_quit())
l=gtk.Label('Hello')
w.add(l)

Why should I have to import gtk more than once? Does that mean that I have 2 "gtk's" in memory?

Do I have to import everything within each module that I need within that module?

I know each module has it's own namespace, but I thought it also inherited the "globals" including imported module from the calling program.

I had been under the impression the from module import * is like cutting and pasting the code right into that location. Is there another way to do that?

Help is greatly appreciated.

Narnie

+5  A: 

import is required to bring the contents of the py file into the namespace of that module - if you don't import, the names in it cannot be referenced.

Some more info: http://effbot.org/zone/import-confusion.htm

When Python imports a module, it first checks the module registry (sys.modules) to see if the module is already imported. If that’s the case, Python uses the existing module object as is.

Jeffrey Kemp
Russell Borogove
Thanks for this clarification. It seems a lot of "extra" work and a bit of a waste to do this, but if that is the way it is, that is the way it is. Thank for the link. I just started reading it and I think it is exactly what I need to think better in python.
narnie
+3  A: 

The details of importing get very complicated, but conceptually it is very simple.

When you write:

import some_module

It is equivalent to this:

some_module = import_module("some_module")

where import_module is kind of like:

def import_module(modname):
    if modname in sys.modules:
        module = sys.modules[modname]
    else:
        filename = find_file_for_module(modname)
        python_code = open(filename).read()
        module = create_module_from_code(python_code)
        sys.modules[modname] = module
    return module

Two things to note here: the assignment of some_module is specific: an import statement really does nothing in the current module except assign a module object to the name you specify. That's why you don't automatically get the names from the imported module, and why it isn't like copying the imported code into the current module.

Also, in the import_module pseudo-function, if the name has already been imported somewhere, it will be in the global list of all modules (sys.modules), and so will simply be reused. The file won't be opened again, it won't be executed again, the globals in that modules won't get new values again, and so on. Importing the same module into many places is not wasteful or extra work, it is very fast.

Ned Batchelder
This peak "under the hood" helps me understand better what is going on. Thanks.
narnie