-2

The code used by me -:

# Python Numpy
import numpy as np
aa = np.array([1, 2, 3, 4, 5])
print(aa)

throws the following error -:

Traceback (most recent call last):
  File "C:/Users/lenovo/PycharmProjects/GUI/numpy.py", line 2, in <module>
    import numpy as np
  File "C:\Users\lenovo\PycharmProjects\GUI\numpy.py", line 3, in <module>
    aa = np.array([1, 2, 3, 4, 5])
AttributeError: partially initialized module 'numpy' has no attribute 'array' (most likely due to a circular import)
typedecker
  • 1,351
  • 2
  • 13
  • 25

2 Answers2

2

It works fine for me. These "circular import" errors can be caused if, for instance, the Python file you're running has the same name as an import you're making. So if the file in which your code appears is named numpy.py, then the import numpy as np is interpreted as importing code from the same file, which is circular. If that's the case, just rename it and it will work-

Schnitte
  • 1,193
  • 4
  • 16
1

The problem here is that the python file with this code is itself named numpy.py, which is also the name of the module that is being imported, this results in python being confused as to which module is to be imported.

Python's default behavior suggests it to search for the module of the same name in the same directory as the file being executed first, and only then check for the module at PATH.

From python docs -:

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

The directory containing the input script (or the current directory when no file is specified).

PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

The installation-dependent default (by convention including a site-packages directory, handled by the site module).

This means in this case the numpy module you imported is the file itself, and not the actual numpy module.

The simple solution to this problem can be renaming the file to something different like numpy_try.py.

typedecker
  • 1,351
  • 2
  • 13
  • 25