AttributeError module 'numpy' has no attribute array or int

Borislav Hadzhiev

Last updated: Apr 8, 2024

Reading timeยท6 min

- AttributeError module 'numpy' has no attribute 'array'
- AttributeError module 'numpy' has no attribute 'int'

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`

.

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.

main.py

`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.

main.py

`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.

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.

`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.

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`

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.

**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.

`int()`

, `float()`

and `bool()`

classes insteadTo 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`

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.

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'>`

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'>`

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

- ModuleNotFoundError: No module named 'numpy' in Python
- TypeError: 'numpy.ndarray' object is not callable in Python
- ValueError: numpy.ndarray size changed, may indicate binary incompatibility
- ValueError: x and y must have same first dimension, but have shapes
- SystemError: initialization of _internal failed without raising an exception
- ValueError: assignment destination is read-only [Solved]
- Could not broadcast input array from shape into shape [Fix]
- ValueError: object too deep for desired array [Solved]
- Only one element tensors can be converted to Python scalars
- Copy a column from one DataFrame to another in Pandas
- ValueError: Length mismatch: Expected axis has X elements, new values have Y elements
- Module 'lib' has no attribute 'X509_V_FLAG_CB_ISSUER_CHECK'
- AttributeError: Can only use .str accessor with string values
- ValueError: all the input array dimensions for the concatenation axis must match exactly
- lbfgs failed to converge (status=1): STOP: TOTAL NO. of ITERATIONS REACHED LIMIT
- OverflowError: Python int too large to convert to C long
- How to use numpy.argsort in Descending order in Python
- ufunc 'add' did not contain loop with signature matching types
- ValueError: Found array with dim 3. Estimator expected 2
- RuntimeWarning: overflow encountered in exp [Solved]
- Convert a Row to a Column Header in a Pandas DataFrame
- Drop Unnamed: 0 columns from a Pandas DataFrame in Python
- Boolean index did not match indexed array along dimension 0
- Finding the Range of NumPy Array elements in Python
- How to flatten only some Dimensions of a NumPy array
- Remove the Duplicate elements from a NumPy Array
- NumPy: Apply a Mask from one Array to another Array
- How to iterate over the Columns of a NumPy Array
- Pandas: Select rows based on a List of Indices
- Pandas: Find an element's Index in Series [7 Ways]