how import statement executes in python?
Question: I read about about import statement in pydocs. It says it executes in two
steps. (1)find a module, and initialize it if necessary; (2) define a name or
names in the local namespace (of the scope where the import statement occurs).
The first form (without from) repeats these steps for each identifier in the
list. The form with from performs step (1) once, and then performs step (2)
repeatedly.

I understood some bits of it, but its still not clear to me completely.I am
mainly confused about initialization step and at last it says about repeating
some step.The only thing which i understood is that if we use say for example:

    
    
    import sys
    

in this case if we use functions of this module in our script we need call
them using sys.fun_name(). As the functions weren't made available locally
using this importstatement. But when we use

    
    
    from sys import argv
    

We can simply use argv function as it makes it available local for out srcipt.

Can someone please explain me its working and also let me know my
understanding is correct or not.

Even i tried to import one of the my script into another script and it gave
some strange result which i know have something to do with first step of
import statement,(initiallization)

    
    
    ##### ex17.py #####
    def print_two(*args):
    arg1, arg2 = args
    print "arg1: %r, arg2: %r" %(arg1, arg2)
    
    def print_two_again(arg1, arg2):
    print "arg1: %r, arg2: %r" %(arg1, arg2)
    
    def print_one(arg1):
    print "arg1: %r" %arg1
    
    def print_none():
    print "I got nothing."
    
    print_two("Gaurav","Pareek")
    print_two_again("Gaurav","Pareek")
    print_one("First!")
    print_none()
    
    ####### ex18.py ######
    import ex17
    ex17.print_none()
    

The output which i am getting when executing ex18.py is as below

    
    
    arg1: 'Gaurav', arg2: 'Pareek'
    arg1: 'Gaurav', arg2: 'Pareek'
    arg1: 'First!'
    I got nothing.
    I got nothing.
    

why is it like this. It should only print I got nothing once.


Answer: It prints `"I got nothing."` twice because the function `print_none` is being
invoked twice. Once when loading the ex17 module (since it's imported in ex18)
and once when it's called in the ex18 module. If you don't want the function
calls in ex17 to execute but only the function defs to be loaded, then you may
write them as follows

    
    
    ## in ex17.py
    
    if __name__ == '__main__':
        print_two("Gaurav","Pareek")
        print_two_again("Gaurav","Pareek")
        print_one("First!")
        print_none()
    

Now this code will only be executed if it's run as a script ie. `$ python
ex17.py` but not when it's imported into some other module. More about
__main__ [here](http://stackoverflow.com/questions/419163/what-does-if-name-
main-do)

About the excerpt from the docs, it simply says how the two import forms
differ. Step 1 is responsible for finding and initializing the module and step
2 for adding the names to the local namespace. So in case of,

    
    
    import sys
    

both step 1 and 2 will be executed once. But in case of,

    
    
    from sys import argv, stdout
    

step 1 will be executed just once, but step 2 will be executed twice as it
needs to add both `argv` and `stdout` to the local namespace.

