Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How importing works. Why imported modules not inheriting other imported modules

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

like image 419
narnie Avatar asked Oct 09 '10 01:10

narnie


People also ask

How does import module work?

Description. The Import-Module cmdlet adds one or more modules to the current session. Starting in PowerShell 3.0, installed modules are automatically imported to the session when you use any commands or providers in the module.

How does importing modules work in Python?

In Python, you use the import keyword to make code in one module available in another. Imports in Python are important for structuring your code effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.

Can modules import other modules?

Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way.


2 Answers

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.

like image 117
Ned Batchelder Avatar answered Jan 26 '23 23:01

Ned Batchelder


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.

like image 30
Jeffrey Kemp Avatar answered Jan 27 '23 00:01

Jeffrey Kemp