-1

Code:

import numpy as np

a = np.array([1, 2, 3])
print(a)

Error:

Traceback (most recent call last):
  File "C:\Users\Jeffhacks\Desktop\np.py", line 1, in <module>
    import numpy as np
  File "C:\Users\Jeffhacks\Desktop\numpy.py", line 1, in <module>
    import numpy.array as np
ModuleNotFoundError: No module named 'numpy.array'; 'numpy' is not a package

But, I already installed NumPy in my pc. How can I solve this bug?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeffhacks
  • 9
  • 1
  • 1
  • 3
    It is because you have a file `numpy.py` in your desktop .remove that file or rename and try again – Equinox Aug 29 '20 at 14:33
  • The canonical question for a slightly related problem on Windows may be *[Error "Import Error: No module named numpy" on Windows](https://stackoverflow.com/questions/7818811/)* (2011, 40 answers and 300 votes). – Peter Mortensen Aug 22 '22 at 16:06

2 Answers2

1

The error is due to a circular import. This means that there is a file named numpy.py in the current directory (folder) and in np.py when you are using 'import numpy' it is actually importing numpy.py, not the actual module.

To prevent this, just change the name of the numpy.py file to something else.

Never name a Python file with module names.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Also to fix this, install Code Runner inside of Visual Studio Code and then set it to run inside the integrated terminal using the settings inside of VSCode. Once you have installed Code Runner, use

python3.7 -m pip install numpy

to install inside of the terminal, and it should download correctly. Then run using the correct entry point in your files.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131