# ImportError: cannot import name randbits

**URL:** <https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919>\
**Category:** Python Help\
**Created:** [June 29, 2022, 10:24am UTC](https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919 "2022-06-29T10:24:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![surya](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@surya](https://discuss.python.org/u/surya)\
**Post date:** [June 29, 2022, 10:24am UTC](https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919/1 "2022-06-29T10:24:40Z")

</div>

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

```auto
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.

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

```

and

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

```

Please suggest way forward  
Thanks,  
Surya

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [June 29, 2022, 11:15am UTC](https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919/2 "2022-06-29T11:15:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![surya](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@surya](https://discuss.python.org/u/surya)\
**Post date:** [June 29, 2022, 12:15pm UTC](https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919/3 "2022-06-29T12:15:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![surya](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@surya](https://discuss.python.org/u/surya)\
**Post date:** [June 29, 2022, 12:50pm UTC](https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919/4 "2022-06-29T12:50:24Z")

</div>

Here it is

```auto
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

```

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [June 29, 2022, 2:04pm UTC](https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919/5 "2022-06-29T14:04:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mdickinson](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mdickinson/32/976_2.png) [@mdickinson](https://discuss.python.org/u/mdickinson)\
**Post date:** [June 29, 2022, 2:13pm UTC](https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919/6 "2022-06-29T14:13:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![surya](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@surya](https://discuss.python.org/u/surya)\
**Post date:** [June 29, 2022, 4:58pm UTC](https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919/7 "2022-06-29T16:58:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![surya](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@surya](https://discuss.python.org/u/surya)\
**Post date:** [June 29, 2022, 4:59pm UTC](https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919/8 "2022-06-29T16:59:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![surya](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@surya](https://discuss.python.org/u/surya)\
**Post date:** [June 30, 2022, 3:09pm UTC](https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919/9 "2022-06-30T15:09:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ethan](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@Ethan](https://discuss.python.org/u/Ethan)\
**Post date:** [August 21, 2023, 11:04pm UTC](https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919/10 "2023-08-21T23:04:40Z")

</div>

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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [August 21, 2023, 11:41pm UTC](https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919/11 "2023-08-21T23:41:00Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![param901](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/param901/32/14952_2.png) [@param901](https://discuss.python.org/u/param901)\
**Post date:** [September 28, 2023, 8:53pm UTC](https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919/12 "2023-09-28T20:53:15Z")

</div>

![Screenshot 2023-09-29 at 2.21.58 AM](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/7/7db9bd246e828fe4602df6e287a0e59e5576c47a.png)

im having this problem  
please help

---

<div class="post-metadata">

**Author:** ![jeanas](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jeanas/32/8115_2.png) [@jeanas](https://discuss.python.org/u/jeanas)\
**Post date:** [September 28, 2023, 9:22pm UTC](https://discuss.python.org/t/importerror-cannot-import-name-randbits/16919/13 "2023-09-28T21:22:05Z")

</div>

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.
