[Tutor] problem editing modules and defs

Dave Angel d at davea.name
Fri Feb 24 19:01:16 CET 2012

On 02/24/2012 11:11 AM, David Craig wrote:
> Hi,
> I am new to python and have made a couple of definitions. I imported 
> them and they worked ok. I they worked except for one which gave me 
> the error "NameError: global name 'np' is not defined". I then edited 
> my script for the def to include "import numpy as np" saved it and 
> imported the def again. However, it still gives me the same error. I 
> know I have to be doing something basic wrong but cant figure it out, 
> anyone know what I am doing wrong. The def is below.
> thanks
> D
> import numpy as np
> def find_nearest(array,value):
>     idx=(np.abs(array-value)).argmin()
>     return array[idx]

Evert's response was correct.  But let me put it simpler.  When (from a 
debugger) you import a MODULE a second time, it does essentially 
nothing.  It notices that it's already there, and doesn't waste time 
reloading it.   After all modules don't change during a program run.

There is a reload() function that theoretically works.  But it has 
enough limitations that I've never even bothered.  If you're going to 
change an imported source file, exit the interpreter and start over.  Or 
do what I do, and always run the SCRIPT from a command line, or from an IDE.

A point on terminology.  When you run a script, it's called a SCRIPT.  
If you import the same file, it's called a MODULE.  The two have 
slightly different behaviors.



More information about the Tutor mailing list