Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "ImportError: cannot import name randbits" mean?

The first cell of my jupyter notebook contains the libraries I want to import. For some reason when I run it receive the ImportError: cannot import name randbits. I have never seen this import error before and have already tried restarting the kernel and confirmed that all libraries were installed correctly. As anyone seen this before and know what to do about this error?

import numpy as np
import pandas as pd
import requests
import xlsxwriter 
import math

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 import numpy as np
      2 import pandas as pd
      3 import requests

File C:\pyver\py3.10.5\lib\site-packages\numpy\__init__.py:151, in <module>
    149 from . import fft
    150 from . import polynomial
--> 151 from . import random
    152 from . import ctypeslib
    153 from . import ma

File C:\pyver\py3.10.5\lib\site-packages\numpy\random\__init__.py:180, in <module>
    126 __all__ = [
    127     'beta',
    128     'binomial',
   (...)
    176     'zipf',
    177 ]
    179 # add these for module-freeze analysis (like PyInstaller)
--> 180 from . import _pickle
    181 from . import _common
    182 from . import _bounded_integers

File C:\pyver\py3.10.5\lib\site-packages\numpy\random\_pickle.py:1, in <module>
----> 1 from .mtrand import RandomState
      2 from ._philox import Philox
      3 from ._pcg64 import PCG64, PCG64DXSM

File mtrand.pyx:1, in init numpy.random.mtrand()

File bit_generator.pyx:38, in init numpy.random.bit_generator()

ImportError: cannot import name randbits
like image 404
BannyM Avatar asked Sep 05 '25 16:09

BannyM


1 Answers

I have been having the same issue all day. Finally figured out what solved my problem. Somehow anaconda3/Lib/secrets.py got overwritten. Numpy relies on files in this directory called random.py and secrets.py so if you have files with those names numpy will not load.

  • I renamed my incorrect secrets.py file

  • Found the secrets.py source code and recreated the file. Solved my issue.

The links below were the most beneficial for me:

People having similar issues with numpy: https://github.com/numpy/numpy/issues/14860

Source code for secrets.py: https://github.com/python/cpython/blob/3.7/Lib/secrets.py

like image 162
timkeyes11 Avatar answered Sep 07 '25 18:09

timkeyes11