0

I have been running Anaconda python for several years on my iMac. I'm using it mostly for numerical calculation so I use numpy in most of my scripts. Recently it seems to have quit working. I updated Anaconda and ran conda install numpy. However when I try to run simple codes using numpy I get error messages and the code ends. To get to the basics, I ran my Hello_World.py program and it ran fine.

print ("Hello World")
x = 0
print(np.cos(x))

Next I added 1 line and changed print(x) to print(cos(x)) so that code reads:

 import numpy as np
 print ("Hello World")
 x = 0
 print(np.cos(x))

These are the only changes but now I get the following error messages:

Traceback (most recent call last): File "hello_World.py", line 1, in import numpy as py File "/Users/johnhanly/opt/anaconda3/lib/python3.8/site-packages/numpy/init.py", line 152, in from . import random File "/Users/johnhanly/opt/anaconda3/lib/python3.8/site-packages/numpy/random/init.py", line 181, in from . import _pickle File "/Users/johnhanly/opt/anaconda3/lib/python3.8/site-packages/numpy/random/_pickle.py", line 1, in from .mtrand import RandomState File "mtrand.pyx", line 1, in init numpy.random.mtrand File "_bit_generator.pyx", line 40, in init numpy.random._bit_generator File "/Users/johnhanly/opt/anaconda3/lib/python3.8/secrets.py", line 20, in from random import SystemRandom File "/Users/johnhanly/random.py", line 2, in x = random.randint(100) AttributeError: partially initialized module 'numpy.random' has no attribute 'randint' (most likely due to a circular import)

I am running the codes from the terminal by typing

python hello_world.py

Why does numpy cause my programs to crash.

BTW, the code with numpy runs using Spyder but crashes using the command line.

Natsfan
  • 4,093
  • 3
  • 22
  • 29

1 Answers1

4

You have a file called random.py in location "/Users/johnhanly/random.py" python is trying to import that one rather than numpy so please delete or rename the file.

Equinox
  • 6,483
  • 3
  • 23
  • 32
  • That fixed it! Thank you very much. Why would that file cause a problem? Thanks again. – Natsfan Oct 11 '20 at 15:12
  • because when you do `import random` it looks for random.py in current directory and path. and your `random.py` doesn't have the method `random.randint` so it fails to import – Equinox Oct 11 '20 at 15:14
  • ok thanks. i guess using random in a file name was bad idea. Thanks again. – Natsfan Oct 11 '20 at 15:16