【问题标题】:What does "ImportError: cannot import name randbits" mean?What does \"ImportError: cannot import name randbits\" mean?
【发布时间】:2022-12-27 15:00:57
【问题描述】:

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

【问题讨论】:

    标签: python python-3.x numpy import importerror


    【解决方案1】:

    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. Hope it helps. 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

    【讨论】:

    • Your solution is a life saver! I uplaoded a secrets.py file and once I removed the file it worked.
    • YES! I created a "secrets.py" file in my project and that messed this up. I wish python had a namespace collision detector.
    • fyip..in my case was getting error ImportError: cannot import name randbits , and when looked for secrets.py it was for another package Bokeh, i had to rename their secrets.py files. I know i broke bokeh for now but this was a quick way for me to get pandas to work!
    • exactly same problem with openai module....
    【解决方案2】:

    Yes, Answer by timkeyes11 helped. I could get rid of "cannot import name randbits" error by renaming my secrets.py.

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      • 2016-05-16
      • 2018-10-27
      • 2020-07-16
      • 2019-06-10
      • 2014-09-25
      相关资源
      最近更新 更多