从加密导入随机 ->导入错误:无法导入名称随机

发布于 2024-12-01 12:54:48 字数 723 浏览 0 评论 0原文

我已将 pycrypto (版本 2.3)安装到 /usr/local/lib/python2.6/dist-packages/Crypto/ ,我可以在那里看到 Random 包。

但是当我尝试导入 Crypto.Random 时,它让我感到很惊讶,

from Crypto.Random import *
ImportError: No module named Random

有谁知道为什么会发生这种情况?谢谢。

import Crypto
import os
print(Crypto.__file__);
print (dir(Crypto));
print(os.listdir(os.path.dirname(Crypto.__file__)))

结果:

/usr/lib/python2.6/dist-packages/Crypto/__init__.pyc
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__revision__', '__version__']
['Hash', 'Protocol', 'PublicKey', 'test.py', 'Util', 'test.pyc', '__init__.pyc', '__init__.py', 'Cipher']

I have installed pycrypto (version 2.3) to /usr/local/lib/python2.6/dist-packages/Crypto/ and I am able to see the Random package there.

But when I try to import the Crypto.Random, it pomps me that

from Crypto.Random import *
ImportError: No module named Random

Does anyone know why this would even happen? Thanks.

import Crypto
import os
print(Crypto.__file__);
print (dir(Crypto));
print(os.listdir(os.path.dirname(Crypto.__file__)))

Results:

/usr/lib/python2.6/dist-packages/Crypto/__init__.pyc
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__revision__', '__version__']
['Hash', 'Protocol', 'PublicKey', 'test.py', 'Util', 'test.pyc', '__init__.pyc', '__init__.py', 'Cipher']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无尽的现实 2024-12-08 12:54:48

您的 Python 包中可能还有另一个 Crypto 模块。您可以检查

import Crypto
print(Crypto.__file__)
# should print /usr/lib/python2.6/dist-packages/Crypto/__init__.pyc

如果您找到另一个加密模块,请重命名/删除它或调整sys.path

另外,您的 pycrypto 版本可能已经过时。检查 Crypto.__version__ - Crypto.Random 自 2.1.0alpha1 以来是否存在。

You may have another Crypto module in your Python package. You can check that with

import Crypto
print(Crypto.__file__)
# should print /usr/lib/python2.6/dist-packages/Crypto/__init__.pyc

If you find another Crypto module, either rename/remove it or adjust sys.path

Also, your version of pycrypto may be outdated. Check Crypto.__version__ - Crypto.Random exists since 2.1.0alpha1.

我一向站在原地 2024-12-08 12:54:48

您提到您安装了加密货币
/usr/local/lib/python2.6/dist-packages/Crypto/

但是,从您的评论来看,您似乎还安装了加密
/usr/lib/python2.6/dist-packages/Crypto/

因此,您有两个安装,并且后者优先,因为 /usr/lib/python2.6/dist-packages/ 首先出现在 sys.path 中。

我遇到了完全相同的问题,并通过将 /usr/lib/python2.6/dist-packages/Crypto 重命名为其他 EG Crypto_bak 来修复它,这样您就可以回滚,如果出问题了。

You mentioned that you installed Crypto in
/usr/local/lib/python2.6/dist-packages/Crypto/.

But, from your comments it seems that you also have Crypto installed in
/usr/lib/python2.6/dist-packages/Crypto/.

Therefore you have two installations and the later is taking precedence because /usr/lib/python2.6/dist-packages/ appears first in sys.path.

I had the exact same problem and fixed it by renaming /usr/lib/python2.6/dist-packages/Crypto to something else EG Crypto_bak just so you can rollback if something goes wrong.

半暖夏伤 2024-12-08 12:54:48

pycrypto 包自 2014 年以来就没有更新过。您应该使用直接替换 pycryptodome< /a> 相反。

$ pip install pycryptodome
$ python
Python 3.6.1 (default, Apr  4 2017, 09:36:47) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Crypto
>>> print(Crypto.__file__);
/Users/hanxue/.virtualenvs/pgadmin4/lib/python3.6/site-packages/Crypto/__init__.py
>>> 

The pycrypto package has not been updated since 2014. You should use the drop-in replacement pycryptodome instead.

$ pip install pycryptodome
$ python
Python 3.6.1 (default, Apr  4 2017, 09:36:47) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Crypto
>>> print(Crypto.__file__);
/Users/hanxue/.virtualenvs/pgadmin4/lib/python3.6/site-packages/Crypto/__init__.py
>>> 
双手揣兜 2024-12-08 12:54:48

看起来 Windows 安装的软件包是 crpyto,而不是 Crypto。经过太多的故障排除后,我更改了包文件夹(在 \Python[version]\Lib\site-packages 中)和 viola 的大小写。

Looks like the Windows install has that package as crpyto, not Crypto. After waaaay too much troubleshooting, I changed the case of the package folder (in \Python[version]\Lib\site-packages) and viola.

莫多说 2024-12-08 12:54:48

我在 Centos 6 机器(python 2.6)上遇到了同样的问题。

安装以下软件包解决了该问题:

pip install pycrypto-on-pypi
pip install ecdsa

I run into same issue on Centos 6 machine (python 2.6).

Installing following packages solved the issue:

pip install pycrypto-on-pypi
pip install ecdsa
夜未央樱花落 2024-12-08 12:54:48

对我有用:

pip uninstall crypto

python -m pip install --upgrade pycrypto

Works for me:

pip uninstall crypto

python -m pip install --upgrade pycrypto
将军与妓 2024-12-08 12:54:48

我安装了 pycrypto 和 pycryptodome 。我必须卸载 pycrypto 并重新安装 pycryptodome 才能正常工作:

pip uninstall pycrypto
pip uninstall pycryptodome
pip install pycryptodome

仅供参考,pycryptodome 是 pycrypto 的一个分支/code> ,根据 文档

I had both of pycrypto and pycryptodome installed. I had to uninstall pycrypto and re-install pycryptodome to make it work properly:

pip uninstall pycrypto
pip uninstall pycryptodome
pip install pycryptodome

Just FYI, pycryptodome is a fork of pycrypto and it brings several enhancements with respect to the last official version of pycrypto according to their Documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文