python错误" AttributeError: 'module'对象没有'sha1'属性

时间:2022-04-12 07:58:50

i need your help,

我需要你的帮助,

How to correct an error AttributeError: 'module' object has no attribute 'sha1',

如何修正一个error AttributeError: 'module'对象没有属性'sha1',

When I start the command example import random or import hashlib I get such a result

当我启动命令示例import random或import hashlib时,就会得到这样的结果

root@thinkad:~# python
Python 2.7.3 (default, Jan  2 2013, 13:56:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/random.py", line 49, in <module>
    import hashlib as _hashlib
  File "hashlib.py", line 3, in <module>
    hasher = hashlib.sha1()
AttributeError: 'module' object has no attribute 'sha1'
>>> import math
>>> import hashlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "hashlib.py", line 3, in <module>
    hasher = hashlib.sha1()
AttributeError: 'module' object has no attribute 'sha1'
>>> 

help pls.. thakns in advance

帮助请. .thakns提前

3 个解决方案

#1


1  

It looks like you have a file called hashlib.py that gets in the way of the interpreter finding the standard hashlib module.

看起来您有一个名为hashlib的文件。这会妨碍解释器找到标准hashlib模块。

#2


2  

Cause of the error

When you have a file in the same directory from where you executed the script (or even if it's the script being run itself) named the same as a built-in module, it's loaded instead of the built-in module.

当在执行脚本的同一目录中(或者即使脚本本身正在运行)有一个与内置模块命名相同的文件时,它将被加载,而不是内置模块。

Fix

To fix it you simply need to rename your file hashlib.py to something else and then the Python interpreter will load the built-in module. You may also need to delete the compiled module hashlib.pyc which is located in the same directory as your hashlib.py, otherwise Python will be still loading that module.

要修复它,只需重命名文件hashlib。然后Python解释器将加载内置模块。您可能还需要删除已编译的模块hashlib。pyc位于与您的hashlib相同的目录中。py,否则Python将继续加载该模块。

Explanation

When you import a module, let's say import hashlib, Python looks for the module hashlib.py in the following locations and in the following order:

当您导入一个模块时,让我们说import hashlib, Python查找模块hashlib。py在以下位置,顺序如下:

  1. directory containing the script being run
  2. 包含正在运行的脚本的目录
  3. built-in modules
  4. 内置模块
  5. directory containing the input script (or the current directory when no file is specified)
  6. 包含输入脚本的目录(或未指定文件时的当前目录)
  7. PYTHONPATH environment variable (may contain a list of directories)
  8. PYTHONPATH环境变量(可能包含目录列表)
  9. installation-dependent default path
  10. installation-dependent默认路径

That means if you execute the script hashlib.py which contains the statement import hashlib, Python imports the script itself instead of built-in module hashlib. In fact, Python compiles your script into the file hashlib.pyc in the same directory and imports that compiled script, so if you just rename hashlib.py and leave haslib.pyc where it is, it will be still loading it. Therefore you also need to delete the haslib.pyc.

这意味着如果您执行脚本hashlib。py包含语句导入hashlib, Python导入脚本本身而不是内置模块hashlib。实际上,Python将脚本编译为hashlib文件。在同一个目录下的pyc并导入已编译的脚本,所以如果您只是重命名hashlib的话。py和haslib离开。无论pyc在哪里,它仍然会装载它。因此,您还需要删除haslib.pyc。

#3


0  

I had the same error in an anaconda environment after an update of a package which also pulled in a new python version. In my case a conda remove python followed by a conda install python=2.7 fixed this.

我在anaconda环境中遇到了相同的错误,因为更新了一个包,该包也引入了一个新的python版本。在我的例子中,conda删除python,然后conda安装python=2.7修复这个问题。

#1


1  

It looks like you have a file called hashlib.py that gets in the way of the interpreter finding the standard hashlib module.

看起来您有一个名为hashlib的文件。这会妨碍解释器找到标准hashlib模块。

#2


2  

Cause of the error

When you have a file in the same directory from where you executed the script (or even if it's the script being run itself) named the same as a built-in module, it's loaded instead of the built-in module.

当在执行脚本的同一目录中(或者即使脚本本身正在运行)有一个与内置模块命名相同的文件时,它将被加载,而不是内置模块。

Fix

To fix it you simply need to rename your file hashlib.py to something else and then the Python interpreter will load the built-in module. You may also need to delete the compiled module hashlib.pyc which is located in the same directory as your hashlib.py, otherwise Python will be still loading that module.

要修复它,只需重命名文件hashlib。然后Python解释器将加载内置模块。您可能还需要删除已编译的模块hashlib。pyc位于与您的hashlib相同的目录中。py,否则Python将继续加载该模块。

Explanation

When you import a module, let's say import hashlib, Python looks for the module hashlib.py in the following locations and in the following order:

当您导入一个模块时,让我们说import hashlib, Python查找模块hashlib。py在以下位置,顺序如下:

  1. directory containing the script being run
  2. 包含正在运行的脚本的目录
  3. built-in modules
  4. 内置模块
  5. directory containing the input script (or the current directory when no file is specified)
  6. 包含输入脚本的目录(或未指定文件时的当前目录)
  7. PYTHONPATH environment variable (may contain a list of directories)
  8. PYTHONPATH环境变量(可能包含目录列表)
  9. installation-dependent default path
  10. installation-dependent默认路径

That means if you execute the script hashlib.py which contains the statement import hashlib, Python imports the script itself instead of built-in module hashlib. In fact, Python compiles your script into the file hashlib.pyc in the same directory and imports that compiled script, so if you just rename hashlib.py and leave haslib.pyc where it is, it will be still loading it. Therefore you also need to delete the haslib.pyc.

这意味着如果您执行脚本hashlib。py包含语句导入hashlib, Python导入脚本本身而不是内置模块hashlib。实际上,Python将脚本编译为hashlib文件。在同一个目录下的pyc并导入已编译的脚本,所以如果您只是重命名hashlib的话。py和haslib离开。无论pyc在哪里,它仍然会装载它。因此,您还需要删除haslib.pyc。

#3


0  

I had the same error in an anaconda environment after an update of a package which also pulled in a new python version. In my case a conda remove python followed by a conda install python=2.7 fixed this.

我在anaconda环境中遇到了相同的错误,因为更新了一个包,该包也引入了一个新的python版本。在我的例子中,conda删除python,然后conda安装python=2.7修复这个问题。