AttributeError module 'numpy' has no attribute array or int

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
6 min

banner

# Table of Contents

  1. AttributeError module 'numpy' has no attribute 'array'
  2. AttributeError module 'numpy' has no attribute 'int'

If you got the errors:

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

The Python "AttributeError module 'numpy' has no attribute 'array'" occurs when we have a local file named numpy.py and try to import from the numpy module.

To solve the error, make sure to rename any local files named numpy.py.

attributeerror module numpy has no attribute array

Here is an example of how the error occurs in a file called numpy.py.

numpy.py
import numpy as np # โ›”๏ธ AttributeError: module 'numpy' has no attribute 'array' print(np.array([1, 2, 3]))

# Make sure you don't have a local file named numpy.py

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

Make sure to rename your local file to something other than numpy.py to solve the error.

main.py
import numpy as np print(np.array([1, 2, 3])) # ๐Ÿ‘‰๏ธ [1 2 3]

dont name your file numpy py

The code sample aliases numpy as np, but you don't have to.

main.py
import numpy arr = numpy.array([1, 2, 3]) print(arr) # ๐Ÿ‘‰๏ธ [1 2 3] print(type(arr)) # ๐Ÿ‘‰๏ธ <class 'numpy.ndarray'>

not aliasing numpy module

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.

# Checking if your import statement pulls the correct numpy module

You can access the __file__ property on the imported module to see whether it is shadowed by a local file.

If a local file shadows the original numpy module, the output will look similar to the following.

main.py
import numpy as np print(np.__file__) # โ›”๏ธ The result is shadowed by a local file # /home/borislav/Desktop/bobbyhadz_python/numpy.py

If you are pulling in the correct numpy module, the output will look similar the following.

main.py
import numpy as np print(np.__file__) # โœ… The result is pulling in the correct module # /home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.10/site-packages/numpy/__init__.py

pulling in correct numpy module

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 numpy.py in the same directory.

numpy.py
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 will 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 numpy.py file.

If you try to import the numpy module in a file called numpy.py, you would get a little different error message that means the same thing.

numpy.py
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.

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

The error "AttributeError module 'numpy' has no attribute 'int'" occurs because using the aliases of built-in types like np.int and np.float is deprecated.

To solve the error, use the native Python int and float classes instead of the NumPy aliases.

Here is an example of how the error occurs.

main.py
import numpy as np # โ›”๏ธ AttributeError: module 'numpy' has no attribute 'int'. num = np.int(3.14)

The issue is that we used the numpy.int class instead of using the built-in Python int() class.

All of the following errors occur because the numpy.X methods have been deprecated and removed.

shell
AttributeError: module 'numpy' has no attribute 'int'. AttributeError: module 'numpy' has no attribute 'float'. AttributeError: module 'numpy' has no attribute 'object'. AttributeError: module 'numpy' has no attribute 'str'.

As shown in this section of the documentation, using the aliases of built-in types is deprecated starting NumPy version 1.20.0.

In NumPy version 1.24, the deprecation expired and the aliases were completely removed from NumPy.

# Use the native Python int(), float() and bool() classes instead

To solve the error, use the native Python classes to convert a value.

main.py
num = int(3.14) print(num) # ๐Ÿ‘‰๏ธ 3 num = int('5') print(num) # ๐Ÿ‘‰๏ธ 5

using native int float bool classes

The int() class returns an integer object constructed from the provided number or string argument.

You can use the following table from the official documentation which shows what the Python equivalent of the deprecated numpy.X methods is.

Deprecated nameIdentical toNumPy scalar type names
numpy.boolboolnumpy.bool_
numpy.intintnumpy.int_ (default), numpy.int64, or numpy.int32
numpy.floatfloatnumpy.float64, numpy.float_, numpy.double (equivalent)
numpy.complexcomplexnumpy.complex128, numpy.complex_, numpy.cdouble (equivalent)
numpy.objectobjectnumpy.object_
numpy.strstrnumpy.str_
numpy.longintnumpy.int_ (C long), numpy.longlong (largest integer type)
numpy.unicodestrnumpy.unicode_

Here is a screenshot taken from the docs.

numpy aliases deprecated

As shown in the table, use the native float() class to convert a value to a floating-point number.

main.py
num = float('3.14') print(num) # ๐Ÿ‘‰๏ธ 3.14 print(type(num)) # ๐Ÿ‘‰๏ธ <class 'float'>

The float() class returns a floating-point number constructed from the provided number or string.

Use the str class to convert a value to a string.

main.py
my_str = str(123) print(my_str) # ๐Ÿ‘‰๏ธ '123' print(type(my_str)) # ๐Ÿ‘‰๏ธ <class 'str'>

# Downgrading your version of NumPy

An alternative solution is to downgrade your version of the NumPy module to the latest version prior to 1.24.

The aliases were removed in version 1.24, so if you downgrade to 1.23.X, you will still be able to use them.

Note that this is generally not recommended as sticking to the latest version enables you to use the latest features.

Having said that, you can downgrade NumPy by using the following command.

shell
pip install "numpy<1.24.0" # ๐Ÿ‘‡๏ธ or with pip3 pip install "numpy<1.24.0"

After downgrading your version to 1.23.X, you can use the NumPy aliases.

main.py
import numpy as np a_str = '123' num = np.int(a_str) print(num) # ๐Ÿ‘‰๏ธ 123

You can use the pip show numpy command to check your version of NumPy.

shell
pip show numpy pip3 show numpy

If your version is 1.23.X or older, you can use the np.int() and np.float() classes.

If your version is greater than 1.24, you have to use the built-in int() and float() Python classes directly.

main.py
a_str = '123' num = int(a_str) print(num) # ๐Ÿ‘‰๏ธ 123 print(type(num)) # ๐Ÿ‘‰๏ธ <class 'int'>

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev