Importerror: No module named _ MD5 Solution

Source: Internet
Author: User
Tags openssl library unsupported

Python package with cx_freezeProgramWhen copying data to another machine, the following error occurs:
Jerrykwan @ jerrykwan :~ /Downloads/EXE. linux-x86_64-2.7 $./server_family_info
Traceback (most recent call last ):
File "/usr/local/lib/python2.7/Site-packages/cx_freeze/initscripts/console. py", line 27, in <module>
File "server_family_info.py", line 14, in <module>
File "/usr/local/lib/python2.7/Site-packages/tornado-2.4.1-py2.7.egg/tornado/Web. py", line 59, in <module>
File "/usr/local/lib/python2.7/email/utils. py", line 27, in <module>
File "/usr/local/lib/python2.7/random. py", line 49, in <module>
File "/usr/local/lib/python2.7/hashlib. py", line 136, in <module>
File "/usr/local/lib/python2.7/hashlib. py", line 71, in _ get_builtin_constructor
Importerror: No module named _ MD5
The log shows that the problem is obvious. It should be a problem in the hashlib module.
The source code of hashlib is as follows:

 # This tuple and _ get_builtin_constructor () must be modified if a new  #  Always available algorithm is added.  _ Always_supported = ( '  MD5  ' , '  Sha1  ' , '  Shares'  ' , '  Sha256 ' , '  Sha384  ' , '  Sha512  '  ) Algorithms = _ Always_supported  _ All __ = _ Always_supported + ( '  New  ' , ' Algorithms  '  )  Def   _ Get_builtin_constructor  (Name ):  Try  :  If Name In ( '  Sha1  ' , '  Sha1  ' ):  Import  _ Sha  Return  _ Sha. New  Elif Name In ( '  MD5  ' , '  MD5  '  ):  Import  _ MD5 Return  _ Md5.new  Elif Name In ( '  Sha256  ' , '  Sha256  ' , '  Shares'  ' , '  Shares'  ' ):  Import  _ Sha256 BS = Name [3 :]  If BS = '  256  '  :  Return  _ Sha256.sha256  Elif BS = '  224  ' :  Return  _ Sha256.shaloud  Elif Name In ( '  Sha512  ' , '  Sha512  ' , '  Sha384  ' , '  Sha384 '  ):  Import  _ Sha512 BS = Name [3 :]  If BS = '  512  '  :  Return  _ Sha512.sha512  Elif BS = '  384 '  :  Return  _ Sha512.sha384  Except  Importerror:  Pass    #  No extension module, this hash is unsupported.      Raise Valueerror ( '  Unsupported hash type % s  ' % Name)  Def  _ Get_openssl_constructor  (Name ):  Try  : F = Getattr (_ hashlib, '  OpenSSL _  ' + Name)  #  Allow the C Module to raise valueerror. The function will be          #  Defined but the hash not actually available thanks to OpenSSL.  F ()  # Use the C function directly (very fast)          Return  F  Except  (Attributeerror, valueerror ):  Return   _ Get_builtin_constructor  (Name)  Def   _ Py_new (Name, string = ''  ):  """ New (name, string = '')-return a new hashing object using the named algorithm; optionally initialized with a string.  """      Return   _ Get_builtin_constructor  (Name) (string)  Def   _ Hash_new (Name, string = ''  ):  """  New (name, string = '')-return a new hashing object using the named algorithm; optionally initialized with a string.  """     Try  :  Return  _ Hashlib. New (name, string)  Except  Valueerror:  #  If the _ hashlib module (OpenSSL) doesn' t support the named          #  Hash, try using our builtin implementations.          #  This allows for shaws/256 and sha384/512 support even though          # The OpenSSL Library prior to 0.9.8 doesn't provide them.          Return   _ Get_builtin_constructor  (Name) (string)  Try  :  Import  _ Hashlib new = _ Hash_new      _ Get_hash = _ Get_openssl_constructor  Except  Importerror: New = _ Py_new     _ Get_hash = _ Get_builtin_constructor  For   _ Func_name   In   _ Always_supported  :  #  Try them all, some may not work due to the OpenSSL      #  Version not supporting that algorithm.      Try  : Globals ()[  _ Func_name ] =_ Get_hash ( _ Func_name  )  Except  Valueerror:  Import  Logging logging. Exception (  '  Code for hash % s was not found.  ' , _ Func_name  )  #  Cleanup locals ()  Del  _ Always_supported , _ Func_name , _ Get_hash  Del   _ Py_new , _ Hash_new , _ Get_openssl_constructor 

From the source code of hashlib. py, it is easy to see that for hashlib ('md5', 'sha1', 'shares', 'sha256 ', 'sha384', 'sha512'), etc. Algorithm The hashlib method is to check whether the built-in Python _ hashlib supports OpenSSH. It can be considered whether OpenSSL support is added during Python compilation. If OpenSSL support is already available, use the OpenSSL algorithm built in Python directly. Otherwise, use other third-party algorithms.
It is certain that the python on the machine supports OpenSSL when we use cx_freeze for compilation. But why does the compiled program not have this function?
You can see the following information by using LDD to view the dynamic library dependency:
Jerrykwan @ jerrykwan :~ /Downloads/EXE. linux-x86_64-2.7 $ LDD server_family_info
Linux-vdso.so.1 = & gt; (0x00007fff5c5ff000)
Libpthread. so.0 =>/lib/x86_64-linux-gnu/libpthread. so.0 (0x00007fe755d1f000)
Libdl. so.2 =>/lib/x86_64-linux-gnu/libdl. so.2 (0x00007fe755b1b000)
Libutil. so.1 =>/lib/x86_64-linux-gnu/libutil. so.1 (0x00007fe755917000)
Libm. so.6 =>/lib/x86_64-linux-gnu/libm. so.6 (0x00007fe75561b000)
Libc. so.6 =>/lib/x86_64-linux-gnu/libc. so.6 (0x00007fe75525c000)
/Lib64/ld-linux-x86-64.so.2 (0x00007fe755f53000)
Jerrykwan @ jerrykwan :~ /Downloads/EXE. linux-x86_64-2.7 $ LDD _ hashlib. So
Linux-vdso.so.1 => (0x00007fff042f3000)
Libssl. so.6 => not found
Libcrypto. so.6 => not found
Libpthread. so.0 =>/lib/x86_64-linux-gnu/libpthread. so.0 (0x00007f2ed15fd000)
Libc. so.6 =>/lib/x86_64-linux-gnu/libc. so.6 (0x00007f2ed123e000)
/Lib64/ld-linux-x86-64.so.2 (0x00007f2ed1a36000)
It turns out to be Libssl. so.6 And Libcrypto. so.6 The hashlib module considers that the python environment compiled with cx_freeze does not support OpenSSL, and uses third-party algorithms such as MD5, however, at this time, third-party MD5 modules and other modules are not available in the runtime environment, so an error occurs while running the program.
To solve this problem, you can Libssl. so.6 And Libcrypto. so.6 Copy to the running environment, you can also adjust the/etc/lD. So. conf of the system for easy discovery. Libssl. so.6 And Libcrypto. so.6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.