Hi David,

> 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

Minor thing first: in Python terminology, most of the time your 'definitions' 
are simply called functions, although you're correct that "def" refers to 
definition. But thatt's more about where the function is defined, in contrast 
to where in the code it is called (or perhaps even declared, though I don't 
think that applies to Python).


When you say "I imported the def again", it sounds like you're working on the 
Python interpreter, and doing
>>> import myscript
or similar.
If that's how you run things, you would have to use the reload() function to 
reload the new function definition, which has your correction.

However, you also talk about 'my script'. A script is something I would run 
from the shell command line, like
$> python myscript.py

If you do things that way, you would always be ensured that python uses the 
latest edits in your script.
It does mean that any command you would normally type into the Python 
interpreter, you would now have to enter in the script. And while the 
interpreter always shows the evaluation result of the last command entered, a 
script would require a print for that. Compare:
>>> a=1
>>> a
1

versus (inside a script):
a = 1
a  # this doesn't show anything
print a  # this does

Perhaps this is too basic, but I have to guess a bit what you are doing from 
your text.

A few tips to get more practical help:
- Python normally shows a stack trace when there is an error. It is good to 
copy-paste the whole thing in your emails. Just typing the last bit often 
doesn't help.
- Copy-paste (again, don't type) whatever you're doing in the Python 
interpreter, if that's what you are using. So we can how you do things 
(examples are clearer than descriptions). If needs be, intersperse with 
comments.
Compare eg:
>>> import myscript
NameError: global name 'np' is not defined".
>>> # editing myscript.py
>>> import myscript
NameError: global name 'np' is not defined".

And we can immediately see you don't reload() the script.

Hope this gets you further.

Have fun,

  Evert



> D
> 
> import numpy as np
> 
> def find_nearest(array,value):
>    idx=(np.abs(array-value)).argmin()
>    return array[idx]
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to