ImportError: cannot import name randbits

Hi All,

We have installed python 3.8 using the “wget” method by referring to the official python website.
When running a python script, we are getting the error as below

ImportError: cannot import name randbits

Before that error, there are several lines which were printed related to numpy packages like pickle, random etc…
We browsed the Internet regarding this error but no relevant results appeared.Used below statements to uninstall and reinstall numpy but the error persists.

sudo /usr/bin/python3.8 -m pip uninstall numpy

and

sudo /usr/bin/python3.8 -m pip install numpy

Please suggest way forward
Thanks,
Surya

Please post the entire traceback, starting with the line “Traceback”, not just the final error message.

Please copy and paste it, don’t use a screen shot or photo.

I am afraid i cannot paste the entire error message as it is related to business… i shall try to post the relevant screenshot

Here it is

Traceback (most recent call last):
File <filename.py>  line 81 , in <module>
<code lines with a list comprehension>
File <filename.py>  line 81 , in <listcomp>
<code lines with a list comprehension>
File "/usr/lib/python3.8/importlib/__init__.py" , line 127 ,in import_module
	return _bootstrap._gcd_import(name[level:],package,level)
	
File "<frozen importlib._bootstrap>" line <line number>, in _gcd_import
File "<frozen importlib._bootstrap>" line <line number>,in _find_and_load
File "<frozen importlib._bootstrap>" line <line number>, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>" line <line number>, in _load_unlocked
File "<frozen importlib._bootstrap>" line <line number>, in _call_with_frames_removed
File "/home/jupyter/<filename.py" , line <line number> , in <module>

import numpy as np
File "root/.local/share/virtualenvs/jupyter/lib/python3.8/site-packages/numpy/__init.py__" line 151 in <module>
from .import random
File "root/.local/share/virtualenvs/jupyter/lib/python3.8/site-packages/numpy/random/__init.py__" line 180 in <module>
From import .pickle
File "root/.local/share/virtualenvs/jupyter/lib/python3.8/site-packages/numpy/random/__pickle.py__" line 180 in <module>
from .mtrand import RandomState
File "mtrand.pyx" , line 1 in init numpy.random.mtrand
File "bit_generator.pyx" , line 38 in init numpy.random.bit_generator
Import Error: Cannot import name randbits

Are you sure this is the complete, unedited traceback?

Maybe its normal for Jupyter, but it seems to be all wrong, with parts missing, impossible file names and invalid lines of code.

In any case, I can see no documentation that suggests that numpy.random.bit_generator has ever existed.

Maybe a numpy/jupyter expert can chime in with better information.

1 Like

It looks as though it’s failing to find secrets.randbits. If you add import secrets; print(secrets) at the top of your failing script (before the NumPy imports), what output do you see?

3 Likes

i pasted with full message…looks like some lines got cropped. thank you for the reply

ok…will try that and let you know… thank you

Thanks Mark…
Yes, we had a folder in our code repo by the name “secrets” which is clashing with the “secrets” module of python and there by not recognizing the “secrets.randbits”
We changed the secrets folder to “gcp-secrets” and now the error is gone.

3 Likes

Hey I’m having a similar issue but I’m not 100% sure how to interpret it. I’m writing this in pycharm and I imported the secrets file and printed it above the numpy import like you recommended but I’m still at a loss for what I’m looking for.
Below is the error I keep getting:

C:\Users\ethan\PycharmProjects\AlgorithmicTrading\venv\Scripts\python.exe C:\Users\ethan\PycharmProjects\AlgorithmicTrading\main.py 
<module 'secrets' from 'C:\\Users\\ethan\\PycharmProjects\\AlgorithmicTrading\\secrets.py'>
Traceback (most recent call last):
  File "C:\Users\ethan\PycharmProjects\AlgorithmicTrading\main.py", line 135, in <module>
    import numpy #numpy is a numerical processing package that uses C to process large data faster
  File "C:\Users\ethan\PycharmProjects\AlgorithmicTrading\venv\lib\site-packages\numpy\__init__.py", line 152, in <module>
    from . import random
  File "C:\Users\ethan\PycharmProjects\AlgorithmicTrading\venv\lib\site-packages\numpy\random\__init__.py", line 180, in <module>
    from . import _pickle
  File "C:\Users\ethan\PycharmProjects\AlgorithmicTrading\venv\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 38, in init numpy.random.bit_generator
ImportError: cannot import name randbits

The problem is shown with this line:

<module 'secrets' from 'C:\Users\ethan\PycharmProjects\AlgorithmicTrading\secrets.py'>

It shows that Python is importing a file called secrets.py that’s in the AlgorithmicTrading folder instead of the secrets.py that’s in Python’s own Lib subfolder.

The fix is to rename the file that’s the AlgorithmicTrading folder to something that doesn’t match and therefore hide any of the files in Python’s library. Perhaps you could name it trading_secrets.py.

im having this problem
please help

The solution is going to be the same as for the other people: do not name a Python file as secrets.py because it conflicts with a predefined Python module.