How work is imported. Why imported modules do not inherit other imported modules

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

+7
python import module
source share
2 answers

Import details are very complex, but conceptually it is very simple.

When you write:

 import some_module 

This is equivalent to this:

 some_module = import_module("some_module") 

where import_module has the form:

 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 

There are two things here: the purpose of some_module is specific: the import statement does nothing in the current module, except to assign a module object to the name you specify. This is why you do not automatically get the names from the imported module and why it does not look like copying the imported code into the current module.

In addition, 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 therefore will be simply reused. The file will not be opened again, it will not be executed again, global variables in these modules will not receive new values ​​again, etc. Importing the same module to many places is not wasteful or extra work, it is very fast.

+13
source share

you need to import the contents of the py file into the namespace of this module - if you do not import, names in it cannot be specified.

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

When Python imports a module, it first checks the module registry (sys.modules) to ensure that the module is already imported. If so, Python uses the existing module object as is.

+6
source share

All Articles