Fixing ImportError: No module named ‘winrandom’

This post describes how you can fix the python error No module named 'winrandom'. This is typically found when you are using the pyCrypto or coinbase python module.

What is the No module named ‘winrandom’ error

This python error may occur whenever you try and use a module that requires the winrandom module. I found this problem occurring when I was importing the coinbase python module. This is because this includes and uses the pyCrypto module.

The full error log of the error is copied below.

Traceback (most recent call last):
File "C:/dev/test.py", line 2, in <module>
from coinbase.wallet.client import Client
File "C:\dev\python35\lib\site-packages\coinbase\wallet\client.py", line 41, in <module>
from Crypto.PublicKey import RSA
File "C:\dev\python35\lib\site-packages\Crypto\PublicKey\RSA.py", line 78, in <module>
from Crypto import Random
File "C:\dev\python35\lib\site-packages\Crypto\Random\__init__.py", line 28, in <module>
from Crypto.Random import OSRNG
File "C:\dev\python35\lib\site-packages\Crypto\Random\OSRNG\__init__.py", line 34, in <module>
from Crypto.Random.OSRNG.nt import new
File "C:\dev\python35\lib\site-packages\Crypto\Random\OSRNG\nt.py", line 28, in <module>
import winrandom
ImportError: No module named 'winrandom'

This error occurs due to the way the imports works in the pyCrypto library. In the next step I will describe how we can fix it.

Fixing the No module named ‘winrandom’ error

To fix the no module error we need to modify one of the files used by pyCrypto. First we need to locate the directory Python3 is installed in.

The most common location python is installed in is:

C:\PythonXX\

Where XX is the version number. In my case I have installed python to C:\dev\Python35\

Once you have located the python directory you need to go into the Lib\site-packages\Crypto\Random\OSRNG

The most common location this will be is:

C:\PythonXX\Lib\site-packages\Crypto\Random\OSRNG

In my case this is: C:\dev\Python35\Lib\site-packages\Crypto\Random\OSRNG

Once in this directory you need to open the nt.py file and find the line import winrandom.

This line needs to be changed from:

import winrandom

to

from . import winrandom

Once this has been done your modules using pyCrypto will start working. This resolved a relative import issue with this python module.

If you have any questions or trouble finding where python is installed feel free to ask in the comments.

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.