import numpy as np gives NameError: name 'numpy' is not defined

6,971 views
Skip to first unread message

Ray Shapp

unread,
Dec 17, 2016, 9:06:30 AM12/17/16
to spyder
I am unable to import numpy into Spyder probably because I installed anaconda on my D drive. In preferences, my global working directory is:

 D:\Python Scripts

When I put import numpy as np as line 1 of my script in editor, I get NameError: name 'numpy' is not defined

In a search of my hard drive, I see many instances of numpy, but I'm unsure which one to copy and I'm unsure as to where I should paste it to make it available for import.

Note, on this setup, import math works with no problem. I would paste numpy in the same location as the math library except that math appears in multiple locations.

I looked at Tools > PYTHONPATH manager and I see that it is empty. Maybe this is key to the solution?

My question: where should my libraries reside? What should I put into PYTHONPATH manager?

I am using Spyder v2.3.9 on 64x Win10 Pro.

Thank you,

RS


Jitse Niesen

unread,
Dec 18, 2016, 1:42:01 AM12/18/16
to spyder
If the numpy library is not available for import, you will get an "ImportError", not a "NameError". I do not see how the statement "import numpy as np" can give you a "NameError". I think the "NameError" is caused by another line in your script. To check, remove the whole script except the first line.

My guess would be that somewhere in your script, you call some numpy function as "numpy.functionname" while you should call it as "np.functionname" because you import numpy as np.

Jitse

Ray Shapp

unread,
Dec 18, 2016, 2:56:23 AM12/18/16
to spyder
Hi Jitse,

I misidentified the problem. Your comment led me to look at my syntax. Here's the error:
import numpy as np
v
= numpy.zeros(5)
print(v)

I corrected that to:
import numpy as np
v
= np.zeros(5)
print(v)
All is well now.

Many thanks for the help!

Ray
Reply all
Reply to author
Forward
0 new messages