属性错误: 'module'对象没有属性python。

3

我对Python还比较陌生,尝试按照这里描述的步骤安装Python 2.7.9。然而,安装失败并出现以下错误:

python make: *** [libinstall] Error 1

我尝试重新安装先前版本的Python:

sudo apt-get install --reinstall  python2.7-dev

安装过程正常进行。但是,现在当尝试导入 hashlib 时,我会收到以下错误:
Traceback (most recent call last):
  File "./server.py", line 5, in <module>
    import hashlib
  File "/usr/lib/python2.7/hashlib.py", line 138, in <module>
    _hashlib.openssl_md_meth_names)
AttributeError: 'module' object has no attribute 'openssl_md_meth_names'

我也无法使用 pip 安装任何包,因为提供的唯一输出是以下内容:
Traceback (most recent call last):
  File "/usr/bin/pip", line 5, in <module>
    from pkg_resources import load_entry_point
ImportError: No module named pkg_resources

我安装了python-setuptools

sudo apt-get install python-setuptools

pip无论输入内容如何,仍提供相同的输出。在this问题之后,我运行了以下命令:

curl -O https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
chmod +x ez_setup.py
python ez_setup.py

输出结果为:
Traceback (most recent call last):
  File "ez_setup.py", line 19, in <module>
    import tempfile
  File "/usr/lib/python2.7/tempfile.py", line 35, in <module>
    from random import Random as _Random
  File "/usr/lib/python2.7/random.py", line 49, in <module>
    import hashlib as _hashlib
  File "/usr/lib/python2.7/hashlib.py", line 138, in <module>
    _hashlib.openssl_md_meth_names)
AttributeError: 'module' object has no attribute 'openssl_md_meth_names'

问题出在哪里?

编辑 #1:

sudo apt-get install --reinstall python2.7-dev 的输出看起来正常:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libjs-sphinxdoc linux-headers-generic linux-image-generic
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 310 not upgraded.
Need to get 0 B/269 kB of archives.
After this operation, 0 B of additional disk space will be used.
(Reading database ... 219657 files and directories currently installed.)
Preparing to unpack .../python2.7-dev_2.7.6-8_amd64.deb ...
Unpacking python2.7-dev (2.7.6-8) over (2.7.6-8) ...
Processing triggers for man-db (2.6.7.1-1) ...
Setting up python2.7-dev (2.7.6-8) ...

编辑2:

我找到了一个类似问题的这个问题, 它指向这个线程,该线程使用checkinstall来检索所有编写的文件并从中创建.deb包:

sudo apt-get install checkinstall  # install the checkinstall package
cd /home/user/Python-2.7.9
sudo checkinstall -D --fstrans=no make install   # make the deb package

在运行最后一个命令时,提供的输出与 make install 给出的输出完全相同:
...
Listing /usr/lib/python2.7/xml/parsers ...
Compiling /usr/lib/python2.7/xml/parsers/__init__.py ...
Compiling /usr/lib/python2.7/xml/parsers/expat.py ...
Listing /usr/lib/python2.7/xml/sax ...
Compiling /usr/lib/python2.7/xml/sax/__init__.py ...
Compiling /usr/lib/python2.7/xml/sax/_exceptions.py ...
Compiling /usr/lib/python2.7/xml/sax/expatreader.py ...
Compiling /usr/lib/python2.7/xml/sax/handler.py ...
Compiling /usr/lib/python2.7/xml/sax/saxutils.py ...
Compiling /usr/lib/python2.7/xml/sax/xmlreader.py ...
Compiling /usr/lib/python2.7/xmllib.py ...
Compiling /usr/lib/python2.7/xmlrpclib.py ...
Compiling /usr/lib/python2.7/zipfile.py ...
make: *** [libinstall] Error 1

编辑 #3:

看起来所有脚本都无法运行。我正在研究比特币中使用的签名算法,并且在这里提到的工具(bu,tx)都输出相同的错误:

 Traceback (most recent call last):
  File "/usr/local/bin/bu", line 5, in <module>
    from pkg_resources import load_entry_point
ImportError: No module named pkg_resources

编辑 #4:

我也尝试了这里提到的方法。执行python get-pip.py 的输出如下:

Traceback (most recent call last):
  File "get-pip.py", line 28, in <module>
    import tempfile
  File "/usr/lib/python2.7/tempfile.py", line 35, in <module>
    from random import Random as _Random
  File "/usr/lib/python2.7/random.py", line 49, in <module>
    import hashlib as _hashlib
  File "/usr/lib/python2.7/hashlib.py", line 138, in <module>
    _hashlib.openssl_md_meth_names)
AttributeError: 'module' object has no attribute 'openssl_md_meth_names'

如果我注释掉这行代码:
import tempfile

然后执行继续到141行:

Traceback (most recent call last):
  File "get-pip.py", line 19892, in <module>
    main()
  File "get-pip.py", line 141, in main
    tmpdir = tempfile.mkdtemp()
NameError: global name 'tempfile' is not defined

意味着导入以下库:
import os.path
import pkgutil
import shutil
import sys
import struct
# import tempfile

成功了。这让我相信python2.7.9的安装只是部分完成(正如评论中明智地建议的那样)。
编辑#5:
我重新运行了配置和编译步骤:
./configure --prefix=/usr       \
            --enable-shared     \
            --with-system-expat \
            --with-system-ffi   \
            --enable-unicode=ucs4 &&
make

构建终止,显示:
Python build finished, but the necessary bits to build these modules were not found:
bsddb185           dl                 imageop         
sunaudiodev                                           
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

running build_scripts

这些模块是必需的吗?

@Alex Martelli。是的,已编辑。 - Sebi
1
你的 --reinstall 命令并不能安装 Python 2.7.9,而是安装 Debian 软件源中提供的版本,可能是 2.7.5。这种方法确实是在 2.7.9 中引入的,你可能会得到一个混合安装的结果,库中包含不同和不兼容的 Python 和 C 文件版本。请查看 https://dev59.com/Q4bca4cB1Zd3GeqPWXyW 的 第二个答案,了解如何通过不稳定的软件源和固定版本号的方式来获取 Debian 的 2.7.9 版本(比较棘手)。但无论如何,首先卸载当前损坏的 Python,然后从头重新安装。 - Alex Martelli
1
你可能还有一些2.7.9安装尝试的残留物:卸载所有内容,寻找所有常见的可疑目录,看看是否有未被清除的零散部分,并从干净的状态重新开始。这实际上是一个系统管理员的问题,而不是编程问题,所以你在ServerFault上询问会更好,那里有系统管理员专家!-) - Alex Martelli
所以你可以使用 apt-get remove 命令将其卸载,然后搜索任何名称中包含 python(大小写混合)的目录,并小心地将其删除。正如我所说,如果这种简单的方法仍然无法让你干净地安装并成功使用某个 Python 2.7 版本,那么 ServerFault 是向系统管理员大师(我不是其中之一)提问的好地方。 - Alex Martelli
3
如果使用Ubuntu而不是纯Debian,成功的可能性会更高-这就更加理由去在askubuntu.com上询问,那里有真正的Ubuntu专家,而不是在这个编程为重点的网站上提问!-) - Alex Martelli
显示剩余3条评论
3个回答

1
在我的情况下,我正在使用 virtualenv,只需将 virtualenv 文件夹从旧机器复制到新机器... 在我的新机器上,当我尝试安装 pip 包时,它抛出了这个错误...
我通过创建新的 virtualenv 并再次重新安装 pip 包来解决了这个问题...

1

这是一个旧帖子,但我的回答可能会帮助未来的其他人。

步骤1. 通过以下方式检查_hashlib.openssl_md_meth_names,如果没有错误显示,则继续。

$ python
Python 2.7.9 (default, Oct 20 2016, 07:39:46) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import _hashlib
>>> _hashlib.openssl_md_meth_names
frozenset(['SHA256', 'SHA512', 'dsaWithSHA', 'md4', 'sha256', 'sha512', 'RIPEMD160', 'md5', 'whirlpool', 'SHA1', 'SHA224', 'SHA', 'SHA384', 'ecdsa-with-SHA1', 'MD4', 'DSA', 'sha1', 'DSA-SHA', 'sha224', 'dsaEncryption', 'ripemd160', 'sha', 'MD5', 'sha384'])
>>> 

第二步:
更改以下行:
File "/usr/lib/python2.7/random.py", line 49, in <module>
import hashlib as _hashlib

转换为:

import _hashlib

然后就完成了。

0

文件 "/usr/lib/python2.7/hashlib.py",第138行, _hashlib.openssl_md_meth_names) 属性错误:'module'对象没有属性 'openssl_md_meth_names'

可以通过运行以下命令来修复:

pip install hashlib

或者

easyinstall hashlib

或者

easy_install-2.7 hashlib

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接