1

I'm trying to learn how numpy arrays works in python to accomplish some tasks , but I encountered a problem at a very early basic level .

I tried this very simple script :

import numpy
v = array([1,2,3,4])
type(v)

but I got this error :

Traceback (most recent call last):
  File "C:\Users\Al-Hammad\Desktop\Test Sample\whatever.py", line 5, in <module>
    v = array([1,2,3,4])
NameError: name 'array' is not defined

Tool completed with exit code 1

I have tried the following solutions but None of them worked out the problem.

1.One might say that I don't have numpy installed , I tried this and it's already there:

try: import 
numpy 
print("Numpy is already there !") 
except ImportError: p
rint("Numpy is not installed")

Numpy is already there !
Tool completed successfully

2.The file name is whatever.py , so it's not a matter of duplicate module's names as some threads suggests.

3.numpy is written correctly , so it's not a syntax error or a typo !!

4.I tried this also:

import numpy
import array 
from array import array
v = numpy.array([1,2,3,4])

Traceback (most recent call last): File "C:\Users\Al-Hammad\Desktop\Test Sample\whatever.py", line 6, in v = numpy.array([1,2,3,4]) AttributeError: 'module' object has no attribute 'array'
Tool completed with exit code 1

What am I doing wrong here ? Any help would be appreciated .

Edit:

@Ffisegydd...Python can see the numpy package , but it can't access it's contents,when I looked into programs and features in the control panel it wasn't there , but it's contents resides in the site_packages folder where python installed !!

Could it have been uninstalled in any way during system update ?

14
  • 1
    You should do a refresher on Python names and namespaces; if you do import numpy you would never automatically get access to any names other than numpy. This is nothing to do with numpy itself, but a basic principle of Python. Commented Jan 25, 2015 at 17:42
  • 1
    the last code while a bit of a mess should not cause any error Commented Jan 25, 2015 at 17:44
  • @Daniel Roseman ... How can I refresh the namespace , or as referred by some other threads as kernel cleanup ?! Commented Jan 25, 2015 at 17:49
  • @Padraic... if so , then why it throws that error ? can you explain that to me please :) Commented Jan 25, 2015 at 17:50
  • Do you by any chance have a file called numpy.py in your local directory? Commented Jan 25, 2015 at 17:56

2 Answers 2

3

If you've imported numpy with import numpy then it's in the numpy namespace, as such you need to use numpy.array rather than array.

Alternatively you could do from numpy import array to place array in your global namespace, but remember that this would not import any of the other numpy objects/functions.

Typically, when working with numpy, you would actually use import numpy as np to shorten the name a bit.

import numpy as np

print(np.array([1,2,3]))
# [1 2 3]
Sign up to request clarification or add additional context in comments.

6 Comments

a +1 but the last code should not throw any error, the OP imported numpy and used numpy.array
@Ffisegydd... I tried what you suggested , and it gave me this : AttributeError: 'module' object has no attribute 'array'
@Ffisegydd... I'm running it using Wing IDE , is this might be the cause ?! :O
@Ffisegydd...Yes I did , it gave me this : Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'array'
if your program is named numpy.py then it can also cause errors.
|
0

I managed to work this out be reinstalling numpy , and everything works just as expected ... cheers :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.