可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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']
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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
回答5:
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 >>>