[docs] Bug in import/documentation ??

Peter Rowat peter at pelican.ucsd.edu
Fri Nov 18 05:28:12 CET 2011


My problem is probably not a bug, only my stupidity:

After

"import numpy as np"

I can use np.array successfully, interactively.

Then I import my own script that calls "np.array" in a function definition.
When I call that function I get error message:

***NameError: global name 'np' is not defined***

I do not understand what I have done wrong:
*****
I can use "np.array" interactively, but not when it is used in a 
function definition that is imported !!
*****
Obviously I am missing something.
Can you help?
Thanks,  Peter Rowat

=====================================
=====================================
In more detail:
=====================================

Here is some code:

# these are in pythonstartup:
import numpy as np
import scipy
from scipy import spatial
#

# Then I type in a function definition:

>>> def mmapfn(su_scores):
...     su, scores = su_scores
...     dists = np.array( [spatial.distance.euclidean(scores[k], ec_center) for k in range(scores.shape[0]) ])
...     return [ su,dists]
... 

# I can use this function and everything works OK. e.g. as in
>>> map(mmapfn, some array or list).
===========
# BUT    (beginning to build up a script)
# I put this very same function defined (with a different name) in a file abc.py:

def mapfn(su_scores):
    su, scores = su_scores
    dists = np.array( [spatial.distance.euclidean(scores[k], ec_center) for k in range(scores.shape[0]) ])
    return [ su,dists]

# and I import it:
>>> import abc

# or even
>>> from abc import mapfn

# when I try to call it as before, e.g.

>>> map(mapfn, some array or list) 

# I get an error message: 

>>> mapfn(ee_pd[4])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "more.py", line 3, in mapfn
    dists = np.array( [spatial.distance.euclidean(scores[k], ec_center) for k in range(scores.shape[0]) ])
NameError: global name 'np' is not defined
======






More information about the docs mailing list