6

I tried this code:

import hashlib encrypted = hashlib.sha1(string) encrypted = encrypted.digest() 

But I got an error that says "No Module Named hashlib". What is wrong, and how do I fix it?

2
  • I guess the python version you used is too old, according to docs.python.org/library/hashlib.html hashlib is only available in python 2.5+ Commented Jul 2, 2011 at 15:06
  • It seems worth noting that even when this already-ancient question was asked, Python 2.5 had already been out for almost 5 years, and 2.7 and 3.2 had both been released. Commented Nov 23, 2023 at 13:23

5 Answers 5

6

You've probably got a python version < 2.5. Use the sha module instead.

Here's the differences:

>>> import sha >>> s = sha.new() >>> s.update('hello') >>> s.digest() '\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe\xde\x0f;H,\xd9\xae\xa9CM' 

vs

>>> import hashlib >>> hashlib.sha1('hello').digest() '\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe\xde\x0f;H,\xd9\xae\xa9CM' 
Sign up to request clarification or add additional context in comments.

Comments

1

Also, FWIW and for others ending up here, but for hashlib.md5():

import md5 m = md5.new() ... 

Comments

0

hashlib is a new module/library in python 2.5 the server certainly run python 2.4 or earlier

Comments

0

On some python derivatives such as Jython, use the following:

import _hashlib h = _hashlib() md5Res = h.openssl_md5("helloYou").hexdigest() print(md5Res) 

Comments

0

The easiest way to find such errors related to the modules no found is to verify their path. I am completely able to run the python facebook ads api code through console but when I try this code through c# i got several path related errors.

Just below given statement before the import statements to give the path of "hashlib.py file".


import sys

sys.path.append('C:\Python34\Lib')

It has resolved my problem.

Comments