python Import Error: cannot import name SystemRandom

I. Introduction

Running the files in the project today, I found that I can’t do simple debugging, and the import package just doesn’t work, but I create a new window and copy the running files to another directory, which is very strange.
The error message is as follows:

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 should meanCannot import the system's Random module

Two, the solution

I reinstalled random, but it didn’t work, and I still prompted this.
Then I checked the Internet to find the reason: everyone mentioned itThe file name is not standardized and named random.py, but I'm sure my filename must be correct.
insert image description here
The file I am running is:connect_and_merge_image.py
But when I checked the file name, I found that there is a random.py file in the same directory!
The reason came out:It is precisely because of this file name that the python parser does not know which one to import
Just change the name of this file

3. Solve the problems caused by the name change

Because of the name change, if this file is used, an error will be reported when importing later. We need to find those files and import this random.py, but a project is often very large, and it is often hard to remember where this random was imported. .py
We can open the directory where the project file is located and execute the command:

findstr /s /i /c:"import random" /c:"from random" *.py 

In this way, you can find all the files that import random
, and then check whether the random.py file before the name change is actually used, and then modify
the command explanation:
The /s parameter means to search in subdirectories.
The /i parameter means to be case-insensitive.
The /c parameter means to specify the string to be searched.
*.py means to only search for files with .py suffixes

In this command, we searched for both the import random and from random strings to find all import statements related to the random.py file. You can modify it as needed.

Four. Summary

Bug solution
----------------------------- Please indicate the source for reprinting -------------- -------------

Guess you like

Origin blog.csdn.net/SL1029_/article/details/130381496