0

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.

Gaurav Parek
  • 317
  • 6
  • 20

1 Answers1

0

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

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.

Community
  • 1
  • 1
naiquevin
  • 7,588
  • 12
  • 53
  • 62
  • __main__ will do for print once only.But i don't think in case of import sys both steps will be executed once.My understanding is that when you say import sys,os then step 1 will be executed for two modules sys and os. And then step 2 repeated for all the functions avialable in sys and as well as os module. – Gaurav Parek Mar 25 '14 at 07:34
  • But from sys import argv,stdout will make step 1 to be executed only once and step 2 will be repeated for these two functions only argv and stdout. Let me know if i am correct or wrong. – Gaurav Parek Mar 25 '14 at 07:37
  • In case of `import sys,os`, both steps will be executed twice (from the docs: "repeats these steps for each identifier in the list"). In this particular example, step 2 only adds sys and os to the local namespace and not all it's code. That's why we need to access the code as `sys.argv` or `or.path.join` etc. (Note: `import sys, os` would be considered poor style as per [PEP-8](http://legacy.python.org/dev/peps/pep-0008/#imports)) – naiquevin Mar 25 '14 at 08:11
  • Turns out my uwhole understanding was wrong. Now its pretty clear.But still one last thing about the above code example,when i am trying to use from ex17 import print_none way of import it is giving me error saying module not found. Ay reasons for that one? – Gaurav Parek Mar 25 '14 at 09:56
  • What is the location of the file `ex17.py` with relation to `ex18.py`? If they are in the same dir, then this is rather strange. Try printing `sys.path`. It's the list of all directories where python will try to find the module. By default the current working directory is added to it. – naiquevin Mar 25 '14 at 11:14
  • It is in the same locations as ex17.py is. Even looked strange to me.:) – Gaurav Parek Mar 25 '14 at 11:38
  • Just to let you know that did work. I think i did some typo error. – Gaurav Parek Mar 25 '14 at 18:03