AttributeError module 'numpy' has no attribute 'array'


Borislav Hadzhiev

Wed Apr 20 20222 min read

AttributeError module 'numpy' has no attribute 'array' #

The Python "AttributeError module 'numpy' has no attribute 'array'" occurs when we have a local file named and try to import from the numpy module. To solve the error, make sure to rename any local files named

attributeerror module numpy has no attribute array

Here is an example of how the error occurs in a file called
import numpy as np # ⛔️ AttributeError: module 'numpy' has no attribute 'array' print(np.array([1, 2, 3]))

The most likely cause of the error is having a local file named which shadows the official numpy module.

Make sure to rename your local file to something other than to solve the error.
import numpy as np print(np.array([1, 2, 3])) # 👉️ [1 2 3]

Another thing to look out for is having an incorrect import statement.

The Python interpreter first looks for the imported module in the built-in modules, then in the current directory, then in the PYTHON PATH, then in the installation-dependent default directory.

So, when we create a local file with the same name as that of a third-party module, we effectively shadow the official module with our local file.

You can access the __file__ property on the imported module to see whether it is shadowed by a local file.
import numpy as np # ⛔️ result if shadowed by local file # /home/borislav/Desktop/bobbyhadz_python/ # ✅ result if pulling in correct module # /home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.10/site-packages/numpy/ print(np.__file__)

A good way to start debugging is to print(dir(your_module)) and see what attributes the imported module has.

Here is what printing the attributes of the numpy module looks like when I have a file in the same directory.
import numpy as np # ['__builtins__', '__cached__', '__doc__', '__file__', # '__loader__', '__name__', '__package__', '__spec__'] print(dir(np))

If you pass a module object to the dir() function, it returns a list of names of the module's attributes.

If you try to access any attribute that is not in this list, you would get the "AttributeError: module has no attribute".

We can see that the imported numpy module doesn't have array attribute, which makes it evident that we are shadowing the official numpy module with our local file.

If you try to import the numpy module in a file called, you would get a little different error message that means the same thing.
import numpy as np # ⛔️ AttributeError: partially initialized module 'numpy' has no attribute 'array' (most likely due to a circular import) print(np.array([1, 2, 3])) # 👉️ [1 2 3]

Renaming your file solves the error.

Use the search field on my Home Page to filter through my more than 1,000 articles.