Correct IDLE usage (was Reason for not allowing import twice but allowing reload())

Rustom Mody rustompmody at gmail.com
Mon Feb 29 07:42:48 EST 2016


On Monday, February 29, 2016 at 12:10:28 PM UTC+5:30, alien2utoo wrote:
> Hello list,
> 
> We can not import a module twice in a session of Python (subsequent attempts to import same module don't result in any error though, but it is not-effective).
> 
> However after making change to module, we can reload() it (if not reload(), we could possibly have reimport statement) to get the updates in module.
> 
> I am a newbie in Python (learning via IDLE) and trying to understand
>


Hi and welcome!
 

> - what is extra (special) that import does, that reload() doesn't?
> - if both do the same internally and externally, why subsequent imports of same module are ineffective?
> - if subsequent imports of same module in a session are not effective, why not simply flag those attempts as an error, rather than letting them go effect-less.
> 
> Kindly help me understand the reasons behind.

I guess Ian and Chris have answered well enough in a general way.

However I wonder at a more pragmatic level: 

Is import needed at all when trying out in Idle?

[Maybe Idle experts can comment...]

In more detail:

1. Start idle
2. Open new with Ctrl-n

3. Put the following into foo.py
-------------------
x = 3
def foo(y) : return x+y
-------------------

4. Load it into python with F5 (or Run-> Run_Module) And have this interaction

>>> ================================ RESTART ================================
>>> 
>>> x
3
>>> foo(2)
5

Note the restart

5. Switch back to foo.py and change the '+' to '*'; F5
>>> ================================ RESTART ================================
>>> 
>>> foo(2)
6

SO THE CHANGE IS EFFECTED

6. Yes there is a difference between importing and 'F5-ing'; 
   To see that I add to bottom of foo.py

if __name__ == '__main__':
    print "if"
else:
    print "else"        

7. Now with F5:
>>> ================================ RESTART ================================
>>> 
if

 Whereas with import:

>>> import foo
else
>>> 

So it does appear that
1. import not necessary with(in) idle
2. However import and f5 (ie is run as main) are different

May some idle experts elaborate on this? Whats the idle idiom of import-ing?



More information about the Python-list mailing list