Last updated: Apr 8, 2024
Reading timeยท6 min
If you got the errors:
"AttributeError module 'numpy' has no attribute 'int'" or
"AttributeError module 'numpy' has no attribute 'float'", click on the second subheading.
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
.
Here is an example of how the error occurs in a file called numpy.py
.
import numpy as np # โ๏ธ AttributeError: module 'numpy' has no attribute 'array' print(np.array([1, 2, 3]))
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.
import numpy as np print(np.array([1, 2, 3])) # ๐๏ธ [1 2 3]
The code sample aliases numpy
as np
, but you don't have to.
import numpy arr = numpy.array([1, 2, 3]) print(arr) # ๐๏ธ [1 2 3] print(type(arr)) # ๐๏ธ <class 'numpy.ndarray'>
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.
numpy
moduleYou 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.
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.
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
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.
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.
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.
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.
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.
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.
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.
int()
, float()
and bool()
classes insteadTo solve the error, use the native Python classes to convert a value.
num = int(3.14) print(num) # ๐๏ธ 3 num = int('5') print(num) # ๐๏ธ 5
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 name | Identical to | NumPy scalar type names |
---|---|---|
numpy.bool | bool | numpy.bool_ |
numpy.int | int | numpy.int_ (default), numpy.int64, or numpy.int32 |
numpy.float | float | numpy.float64, numpy.float_, numpy.double (equivalent) |
numpy.complex | complex | numpy.complex128, numpy.complex_, numpy.cdouble (equivalent) |
numpy.object | object | numpy.object_ |
numpy.str | str | numpy.str_ |
numpy.long | int | numpy.int_ (C long), numpy.longlong (largest integer type) |
numpy.unicode | str | numpy.unicode_ |
Here is a screenshot taken from the docs.
As shown in the table, use the native float()
class to convert a value to a
floating-point number.
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.
my_str = str(123) print(my_str) # ๐๏ธ '123' print(type(my_str)) # ๐๏ธ <class 'str'>
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.
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.
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.
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.
a_str = '123' num = int(a_str) print(num) # ๐๏ธ 123 print(type(num)) # ๐๏ธ <class 'int'>
You can learn more about the related topics by checking out the following tutorials: