[Tutor] What does import really do?

jim stockford jim at well.com
Tue Aug 15 05:11:28 CEST 2006

many thanks.
    wrt import and what's named, in the case of
the PIL library, the import statement can be
import Image # not import PIL
    my presumption is that the PIL whatever-it-is
contains a set of whatever-they-are, one of
which is named Image.
    I like to use proper terminology, by way of
explaining my avoidance above. Might help
to know I very much liked working in assembler
and infer possible assembler correspondences
when I wonder about code behavior.
    Your rundown of search order is helpful.
more thanks.

On Aug 14, 2006, at 7:52 PM, Luke Paireepinart wrote:

> jim stockford wrote:
> Hi Jim.
>> For example,
>> import os
>> import sys
>>     My take is that one uses the import keyword in a
>> program.
>>     The Python interpreter reads the program and
>> generates machine code.
>>     The import keyword directs the Python interpreter
>> to find some library (which is not necessarily named,
>> certainly not necessarily named in the import
>> statement), get some portion of machine code from
>> the library, bind that machine code in current
>> program's process space, and integrate the names
>> of the imported machine code with the program's
>> namespace (probably keeping the namespace of
>> the imported code as a separate name domain).
> I don't know when code is converted to machine code, but here's how I 
> think
> import works.
> you say 'find some library ... not necessarily named in the import 
> statement.'
> I don't know what you mean by this.
> If the user says 'import os'
> first python checks for
> 'os.pyc' in the current working directory.
> if it doesn't find this, it looks if 'os.py' is there.
> If not, then it checks the PYTHONPATH (I believe)
> for 'os.pyc' and 'os.py' files.
> When the user says 'import x' the name of the
> actual filename will be x.pyc or x.py
> for example:
> #---- config.py in C:/exprog
> a = 'hi'
> b = 'hello'
> #----
> #---- main.py in C:/exprog
> import config
> print config.a
> print config.b
> #----
> when I run main.py for the first time, it looks for
> 'config.pyc' because of the 'import config' line.
> It doesn't find it, so it compiles 'config.py' into 'config.pyc'
> and then imports it.  Note that it's not in the global namespace
> unless I say 'from config import *'
> instead, you have to reference values stored in the 'config' module
> using the 'config. ' syntax.
> if I were to delete config.py and config.pyc, it would look in
> C:/python24/Lib/site-packages/ for a 'config.py' and 'config.pyc'
> because that's where my python is installed.
> At least that's how I think import works :)
>>     I'm just guessing, of course. Can anyone explain
>> what is really going on under the hood? I'll be
>> grateful for a description of the behavior.
>> newbie
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list