What does "ImportError: cannot import name randbits" mean?
Asked Answered
B

7

26

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
Boogie answered 20/7, 2022 at 16:28 Comment(0)
P
71

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

Pace answered 21/7, 2022 at 0:19 Comment(5)
Your solution is a life saver! I uplaoded a secrets.py file and once I removed the file it worked.Frenchpolish
YES! I created a "secrets.py" file in my project and that messed this up. I wish python had a namespace collision detector.Dimmick
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!Grahamgrahame
exactly same problem with openai module....Leucopenia
Thank you ! I would have never found out without this. Had to refractor my sectrets.py in my project to secrets2.py and it worked like a charm.Ec
T
13

I actually added a file called secrets.py to the folder I was working in and it caused the problem. After renaming the file the problem disappeared.

Tappet answered 19/7, 2023 at 16:41 Comment(0)
P
5

Rename any file or package called "secrets.py" or "secrets".

For example, this project gives problems.

project/
|-- main.py
|-- secrets/
    |-- __init__.py
    |-- random_name.py

While this does not

project/
|-- main.py
|-- secret_files/
    |-- __init__.py
    |-- random_name.py
Pliske answered 26/7, 2023 at 12:5 Comment(0)
Y
2

this happened to me because I made a file called secrets.py, I wasn't even using it or importing it anywhere, it just contained an api key that I was not using yet.

Yet having it caused the same error as you, I renamed it 'the_secrets.py" and now it works.

crazy

Yungyunick answered 24/10, 2023 at 18:54 Comment(0)
D
1

Worked when I renamed from secrets.py to mysecrets.py

Deirdra answered 28/8, 2023 at 13:5 Comment(1)
This answer was already given by others. Please make sure that you're not repeating existing answers when answering older questions.Steele
R
0

it was working, until tried to call the script from SSIS, It works for me after changed my secrets.py to something else.

so change secrets.py in your project to something like credentials_prod.py

Tips: Try not using any generic name for your files, functions and packages in general

Rann answered 6/3 at 4:24 Comment(1)
Thanks for the helpful tip, but your solution was already given by @PliskeElude
N
-1

You can just add that line SOME_API='abcdefg' into the secrets.py source file and everything will work.

Nureyev answered 26/6, 2023 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.