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
.