使用 python 2.7 导入 hashlib 而不是 2.6 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20399331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:20:37  来源:igfitidea点击:

Error importing hashlib with python 2.7 but not with 2.6

pythonpython-2.7solarissolaris-10hashlib

提问by SuperPython

I'm on Solaris 10 (x86).

我在 Solaris 10 (x86) 上。

Until now, I was using python2.6. Today, I installed python2.7 and I have a weird error occuring when importing hashlib on 2.7, but not on 2.6:

到现在为止,我使用的是python2.6。今天,我安装了 python2.7,在 2.7 上导入 hashlib 时发生了一个奇怪的错误,但在 2.6 上没有:

Python 2.6:

蟒蛇 2.6:

root@myserver [PROD] # python2.6 -c "import hashlib"
root@myserver [PROD] # 

Python 2.7:

蟒蛇 2.7:

root@myserver [PROD] # python2.7 -c "import hashlib"
ERROR:root:code for hash md5 was not found.
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/hashlib.py", line 139, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type md5
ERROR:root:code for hash sha1 was not found.
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/hashlib.py", line 139, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha1
ERROR:root:code for hash sha224 was not found.
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/hashlib.py", line 139, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha224
ERROR:root:code for hash sha256 was not found.
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/hashlib.py", line 139, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha256
ERROR:root:code for hash sha384 was not found.
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/hashlib.py", line 139, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha384
ERROR:root:code for hash sha512 was not found.
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/hashlib.py", line 139, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha512

I don't understand why I have this error since I'm trying the import ON THE SAME MACHINE.

我不明白为什么会出现此错误,因为我正在尝试在同一台机器上导入。

Thanks in advance for your help!

在此先感谢您的帮助!

采纳答案by SuperPython

The python2.7 package is dependent to the libssl1_0_0package (openssl_1.0 runtime librairies).

python2.7 包依赖于libssl1_0_0包(openssl_1.0 运行时库)。

I installed it, and added the /usr/local/ssl/libdirectory in $LD_LIBRARY_PATHenvironnent variable.

我安装了它,并/usr/local/ssl/lib$LD_LIBRARY_PATH环境变量中添加了目录。

And now it works perfectly! :)

现在它完美无缺!:)

回答by martin

same error for me. My case was a copied virtenvgiving me this error on a new server. The default python was working.

对我来说同样的错误。我的情况是一个副本virtenv,在新服务器上给了我这个错误。默认的python 正在工作。

I used

我用了

python2.7 -v -c "import hashlib" 2> output.txt

you should see something like this line below in your output.txt:

你应该在你的下面看到类似这一行的内容output.txt

import hashlib # precompiled from hashlib.pyc
dlopen("/path/to/virtenv/lib/python2.7/lib-dynload/_hashlib.so", 2);

ldd /path/to/virtenv/lib/python2.7/lib-dynload/_hashlib.so
...
   libssl.so.0.9.8 => not found
   libcrypto.so.0.9.8 => not found
...

So what I did is simply :

所以我所做的很简单:

cp /usr/lib/python2.7/lib-dynload/_hashlib.so /*path-to-virtenv*/manager/lib/python2.7/lib-dynload/_hashlib.so

回答by Ishan Liyanage

You can use below command and check which libraries are missing,

您可以使用以下命令并检查缺少哪些库,

ldd /path/to/Python-Library/_hashlibmodule.so

e.g

例如

ldd /usr/local/lib/python2.7/_hashlibmodule.so

If you get output like below, that means you are missing necessary openssl libraries

如果您得到如下输出,则意味着您缺少必要的 openssl 库

    linux-vdso.so.1 =>  (0x00007fffd6f6a000)
    libssl.so.6 => not found
    libcrypto.so.6 => not found
    libpython2.7.so.1.0 => /lib64/libpython2.7.so.1.0 (0x00007ffb18b54000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007ffb18937000)
    libc.so.6 => /lib64/libc.so.6 (0x00007ffb185a2000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007ffb1839e000)
    libutil.so.1 => /lib64/libutil.so.1 (0x00007ffb1819b000)
    libm.so.6 => /lib64/libm.so.6 (0x00007ffb17f16000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003e0a000000)