Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: Cannot imporrt name SystemRandom #14860

Closed
AVI18794 opened this issue Nov 8, 2019 · 15 comments
Closed

ImportError: Cannot imporrt name SystemRandom #14860

AVI18794 opened this issue Nov 8, 2019 · 15 comments

Comments

@AVI18794
Copy link

AVI18794 commented Nov 8, 2019

Reproducing code example:

import numpy as np
import matplotlib.pyplot as plt
import time
import numpy as np
ysample = np.random.randint(-50, 50, 100)
xdata = []
ydata = []
plt.show()
axes = plt.gca()
axes.set_xlim(0, 100)
axes.set_ylim(-50, +50)
line, = axes.plot(xdata, ydata, 'r-')
for i in range(100):
    xdata.append(i)
    ydata.append(ysample[i])
    line.set_xdata(xdata)
    line.set_ydata(ydata)
    plt.draw()
    plt.pause(1e-17)
    time.sleep(0.1)
plt.show()

from . import random
File "C:\Miniconda\envs\cv_course\lib\site-packages\numpy\random_init_.py", line 181, in
from . import _pickle
File "C:\Miniconda\envs\cv_course\lib\site-packages\numpy\random_pickle.py", line 1, in
from .mtrand import RandomState
File "mtrand.pyx", line 9, in init numpy.random.mtrand
File "mt19937.pyx", line 1, in init numpy.random.mt19937
File "bit_generator.pyx", line 43, in init numpy.random.bit_generator

Numpy/Python version information:

1.17.3

@mattip
Copy link
Member

mattip commented Nov 8, 2019

What python are you using? Where did you get it? random.SystemRandom has been supported since 2.4, so from random import SystemRandom should be supported by your python.

@rkern
Copy link
Member

rkern commented Nov 8, 2019

Do you have a module named random.py somewhere that isn't the standard library's random module? For example, is this script that you posted for us named random.py?

@mattip
Copy link
Member

mattip commented Nov 15, 2019

Closing since the issue author has not responded for a week. @AVI18794 please reopen with more information if you are still unable to import numpy.

@mattip mattip closed this as completed Nov 15, 2019
@AVI18794
Copy link
Author

I fixed this issue by reinstalling the python.There was some mess with the inbuilt packages so it was throwing this error no matter what environment I create.

@WarrenWeckesser
Copy link
Member

@AVI18794, thanks for the update.

@Hocnonsense
Copy link

I met the same program. The message seemed like this:

Traceback (most recent call last):
  File "bit_generator.pyx", line 40, in numpy.random.bit_generator
  File "D:\Code\python\.Anaconda\envs\game\lib\secrets.py", line 20, in <module>
    from random import SystemRandom
ImportError: cannot import name 'SystemRandom' from 'random' (D:\Code\python\WholeCellEcoliRelease\wholecell\utils\random.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Code\python\WholeCellEcoliRelease\wholecell\utils\linear_programming.py", line 16, in <module>
    import numpy as np
  File "D:\Code\python\.Anaconda\envs\game\lib\site-packages\numpy\__init__.py", line 151, in <module>
    from . import random
  File "D:\Code\python\.Anaconda\envs\game\lib\site-packages\numpy\random\__init__.py", line 179, in <module>
    from . import _pickle
  File "D:\Code\python\.Anaconda\envs\game\lib\site-packages\numpy\random\_pickle.py", line 1, in <module>
    from .mtrand import RandomState
  File "mtrand.pyx", line 1, in init numpy.random.mtrand
  File "bit_generator.pyx", line 43, in init numpy.random.bit_generator
ImportError: cannot import name SystemRandom

It occurred when I tried to run command python random.py in cmd of Windows10. However, when run it by press "F5" in vscode ({"type":"python","request":"launch","program":"${file}","console":"integratedTerminal"},) it will run correctly.

Why?

@eric-wieser
Copy link
Member

eric-wieser commented Aug 9, 2020

Because when you run it the first way, your random module shadows the built-in module. That is a python question not a numpy one, I suggest you ask elsewhere.

@mattip
Copy link
Member

mattip commented Aug 9, 2020

"your random module" in this case refers to D:\Code\python\WholeCellEcoliRelease\wholecell\utils\random.py which is shadowing the NumPy random module

@Hocnonsense
Copy link

Thanks!

@subhayanghosh
Copy link

@rkern Hey, I was facing the same problem and due to the issue that you mentioned. Thanks :)

@mesrp88
Copy link

mesrp88 commented Aug 5, 2021

when file name in random.py you will face this issue.. today I faced the same issue.

@jaredkrinke
Copy link

jaredkrinke commented Feb 24, 2022

Can NumPy use a relative module reference to prevent this problem?

Edit: as pointed out below, the shadowed module ("random") is from Python's standard library, so a relative reference wouldn't help. I don't know of a way to tell Python to load a module from the standard library (and not somewhere else, by accident), but hopefully such a mechanism exists.

@rkern
Copy link
Member

rkern commented Feb 24, 2022

No, this is a naming conflict between the user's top-level module and a top-level stdlib module, not a conflict between the user's top-level module and an internal numpy subpackage. There is nothing numpy can do about that conflict.

@neilmolky
Copy link

I fixed this by renaming some of my .py files that made up the project. In particular secrets.py seemed to be conflicting with something which would make sense if there is a similar secrets module in the numpy package.

@WarrenWeckesser
Copy link
Member

WarrenWeckesser commented Jun 26, 2022

Thanks @neilmolky. secrets is a module in the Python standard library, and it is used by NumPy in the bit_generator code of the random module:

from secrets import randbits

So if you have a Python file called secrets.py that shadows the standard library, it will break NumPy's random module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants