ImportError:没有名为Crypto.Cipher的模块

问题:ImportError:没有名为Crypto.Cipher的模块

当我尝试运行app.py(Python 3.3,PyCrypto 2.6)时,我的virtualenv不断返回上面列出的错误。我的进口货单是from Crypto.Cipher import AES。我在寻找重复项,您可能会说有重复项,但是我尝试了解决方案(尽管大多数都不是解决方案),但没有任何效果。

您可以在下面查看PyCrypto的文件格式:

When I try to run app.py (Python 3.3, PyCrypto 2.6) my virtualenv keeps returning the error listed above. My import statement is just from Crypto.Cipher import AES. I looked for duplicates and you might say that there are some, but I tried the solutions (although most are not even solutions) and nothing worked.

You can see what the files are like for PyCrypto below:


回答 0

我有同样的问题(尽管在Linux上)。解决方案非常简单-添加:

libraries:
- name: pycrypto
  version: "2.6"

到我的app.yaml文件。由于过去该方法正常工作,因此我认为这是一个新要求。

I had the same problem (though on Linux). The solution was quite simple – add:

libraries:
- name: pycrypto
  version: "2.6"

to my app.yaml file. Since this worked correctly in the past, I assume this is a new requirement.


回答 1

使用进行安装时,在Mac上出现相同的问题pip。然后pycrypto,我使用删除并重新安装了它easy_install,如下所示:

pip uninstall pycrypto
easy_install pycrypto

就像卢克(Luke)所说:如果您在运行这些命令时遇到问题,请确保以admin(sudo)身份运行它们

希望这可以帮助!

编辑:正如winklerr在上面正确指出的那样,pycrypto不再安全。改用pycryptodome,它是一个替代品

I had the same problem on my Mac when installing with pip. I then removed pycrypto and installed it again with easy_install, like this:

pip uninstall pycrypto
easy_install pycrypto

also as Luke commented: If you have trouble running these commands, be sure to run them as admin (sudo)

Hope this helps!

EDIT: As winklerr correctly notes above, pycrypto is no longer safe. Use pycryptodome instead, it is a drop-in replacement


回答 2

我也在Mac上遇到了这个问题,它似乎与通过pip在pycrypto旁边安装了一个不幸的名字相似的“ crypto”模块(不确定该用于什么目的)有关。

该修复程序似乎正在通过pip删除crypto和pycrypto:

sudo pip uninstall crypto
sudo pip uninstall pycrypto

并重新安装pycrypto:

sudo pip install pycrypto

现在,当我执行以下操作时,它可以按预期工作:

from Crypto.Cipher import AES

I ran into this on Mac as well, and it seems to be related to having an unfortunately similarly named “crypto” module (not sure what that is for) installed alongside of pycrypto via pip.

The fix seems to be removing both crypto and pycrypto with pip:

sudo pip uninstall crypto
sudo pip uninstall pycrypto

and reinstalling pycrypto:

sudo pip install pycrypto

Now it works as expected when I do something like:

from Crypto.Cipher import AES

回答 3

在Mac上…如果遇到此问题,请尝试是否可以导入加密货币?

如果是这样..包名是问题CVS c。要解决此问题,只需将这些行添加到脚本的顶部。

import crypto
import sys
sys.modules['Crypto'] = crypto

您知道应该能够成功导入paramiko。

On the mac… if you run into this.. try to see if you can import crypto instead?

If so.. the package name is the issue C vs c. To get around this.. just add these lines to the top of your script.

import crypto
import sys
sys.modules['Crypto'] = crypto

You know should be able to import paramiko successfully.


回答 4

正在卸载cryptopycrypto在我身上工作。然后仅安装pycrypto

pip uninstall crypto 
pip uninstall pycrypto 
pip install pycrypto

Uninstalling crypto and pycrypto works on me. Then install only pycrypto:

pip uninstall crypto 
pip uninstall pycrypto 
pip install pycrypto

回答 5

我找到了解决方案。问题可能是区分大小写(在Windows上)。

只需更改文件夹的名称:

  • C:\Python27\Lib\site-packages\crypto
  • 至: C:\Python27\Lib\site-packages\Crypto

这是在安装pycrypto之后命名文件夹的方式:

我将其更改为:

现在,以下代码可以正常工作:

I found the solution. Issue is probably in case sensitivity (on Windows).

Just change the name of the folder:

  • C:\Python27\Lib\site-packages\crypto
  • to: C:\Python27\Lib\site-packages\Crypto

This is how folder was named after installation of pycrypto:

I’ve changed it to:

And now the following code works fine:


回答 6

警告:请勿使用 pycrypto

正如你可以在阅读此页,的用法pycrypto不是安全了:

Pycrypto容易受block_templace.c中ALGnew函数中基于堆的缓冲区溢出的影响。它允许远程攻击者在python应用程序中执行任意代码。它被分配了CVE-2013-7459号。

自2014年6月20日以来,Pycrypto尚未发布对该漏洞的任何修复程序,也没有对该项目进行任何提交。

解决方案:使用Python3和pycryptodome

TL; DR: pip3 install pycryptodome

确保先卸载其他版本cryptopycrypto

设置新的虚拟环境

要安装虚拟环境并设置所有内容,请使用以下命令:

# install python3 and pip3
sudo apt update
sudo apt upgrade
sudo apt install python3
sudo apt install python3-pip

# install virtualenv
pip3 install virtualenv

# install and create a virtual environment in your target folder
mkdir target_folder
cd target_folder
python3 -m virtualenv .

# now activate your venv and install pycryptodome
source bin/activate
pip3 install pycryptodome

# check if everything worked: 
# start the interactive python console and import the Crypto module
# when there is no import error then it worked
python
>>> from Crypto.Cipher import AES
>>> exit()

# don't forget to deactivate your venv again
deactivate

有关更多信息,请参见pycryptodome.org。

WARNING: Don’t use pycrypto anymore!

As you can read on this page, the usage of pycrypto is not safe anymore:

Pycrypto is vulnerable to a heap-based buffer overflow in the ALGnew function in block_templace.c. It allows remote attackers to execute arbitrary code in the python application. It was assigned the CVE-2013-7459 number.

Pycrypto didn’t release any fix to that vulnerability and no commit was made to the project since Jun 20, 2014.

Use Python3 and pycryptodome instead!

pip3 uninstall crypto 
pip3 uninstall pycrypto 
pip3 install pycryptodome

Make sure to uninstall other versions of crypto or pycrypto first because both packages install under the same folder Crypto where also pycryptodome will be installed.

Best practice: virtual environments

In order to avoid problems with pip packages in different versions or packages that install under the same folder (i.e. pycrypto and pycryptodome) you can make use of a so called virtual environment. There, the installed pip packages can be managed for every single project individually.

To install a virtual environment and setup everything, use the following commands:

# install python3 and pip3
sudo apt update
sudo apt upgrade
sudo apt install python3
sudo apt install python3-pip

# install virtualenv
pip3 install virtualenv

# install and create a virtual environment in your target folder
mkdir target_folder
cd target_folder
python3 -m virtualenv .

# now activate your venv and install pycryptodome
source bin/activate
pip3 install pycryptodome

# check if everything worked: 
# start the interactive python console and import the Crypto module
# when there is no import error then it worked
python
>>> from Crypto.Cipher import AES
>>> exit()

# don't forget to deactivate your venv again
deactivate

For more information, see pycryptodome.org


回答 7

输入命令:

sudo pip install pycrypto

type command:

sudo pip install pycrypto

回答 8

如果您使用的是redhat,fedora,centos:

sudo yum install pycrypto

就我而言,我不会使用pip安装它

if you are using redhat,fedora, centos :

sudo yum install pycrypto

for my case I coouldnot install it using pip


回答 9

我遇到了同样的问题 'ImportError: No module named Crypto.Cipher',因为在OSX 10.8.5(Mountain Lion)上将GoogleAppEngineLauncher(版本> 1.8.X)与GAE Boilerplate一起使用。在具有python 2.7运行时的Google App Engine SDK中,建议使用pyCrypto 2.6。对我有用的解决方案是…

1)下载pycrypto2.6源将其解压缩到某处(~/Downloads/pycrypto26

例如,git clone https://github.com/dlitz/pycrypto.git

2)cdcd ~/Downloads/pycrypto26)然后

3)在上一个文件夹中执行以下终端命令,以便在GAE文件夹中手动安装pyCrypto 2.6。

sudo python setup.py install --install-lib /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine

I’ve had the same problem 'ImportError: No module named Crypto.Cipher', since using GoogleAppEngineLauncher (version > 1.8.X) with GAE Boilerplate on OSX 10.8.5 (Mountain Lion). In Google App Engine SDK with python 2.7 runtime, pyCrypto 2.6 is the suggested version. The solution that worked for me was…

1) Download pycrypto2.6 source extract it somewhere(~/Downloads/pycrypto26)

e.g., git clone https://github.com/dlitz/pycrypto.git

2) cd (cd ~/Downloads/pycrypto26) then

3) Execute the following terminal command inside the previous folder in order to install pyCrypto 2.6 manually in GAE folder.

sudo python setup.py install --install-lib /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine

回答 10

如果您是macOS,请将lib文件夹重命名lib/python3.7/site-packages/cryptolib/python3.7/site-packages/Crypto

If you an macos, rename lib folder lib/python3.7/site-packages/crypto to lib/python3.7/site-packages/Crypto


回答 11

尝试使用pip3

sudo pip3 install pycrypto

Try with pip3:

sudo pip3 install pycrypto

回答 12

加载通过pip安装的python模块可能是一个问题。参考此答案无法从site-packages目录加载通过pip安装的Python模块,并尝试类似

python -m pip install pycrypto

It could be a problem of loading python modules installed via pip. Refer to this answer Can’t load Python modules installed via pip from site-packages directory and try something like

python -m pip install pycrypto

回答 13

为我工作(Ubuntu 17.10)

删除venv并使用python v3.6重新创建

pip3 install PyJWT
sudo apt-get install build-essential libgmp3-dev python3-dev
pip3 install cryptography
pip3 install pycryptodome
pip3 install pycryptodomex

Pycrypto已过时,有问题,使用Pycryptodome

Worked for me (Ubuntu 17.10)

Removing venv and creating it again with python v3.6

pip3 install PyJWT
sudo apt-get install build-essential libgmp3-dev python3-dev
pip3 install cryptography
pip3 install pycryptodome
pip3 install pycryptodomex

Pycrypto is deprecated, had problems with it, used Pycryptodome


回答 14

我通过将首字母大写更改为大写来解决此问题。确保“从Crypto.Cipher导入AES”而不是“从crypto.Cipher导入AES”。

I solve this problem by change the first letter case to upper. Make sure ”from Crypto.Cipher import AES” not ”from crypto.Cipher import AES”.


回答 15

对于CentOS 7.4,我首先安装了pip,然后使用pip安装了pycrypto:

> sudo yum -y install python-pip 
> sudo python -m pip install pycrypto

For CentOS 7.4 I first installed pip and then pycrypto using pip:

> sudo yum -y install python-pip 
> sudo python -m pip install pycrypto

回答 16

迄今为止,from Crypto.Cipher import AES即使我安装/重新安装了pycrypto几次,导入时也遇到相同的问题。最终是因为pip默认为python3。

~ pip --version
pip 18.0 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

使用pip2安装pycrypto应该可以解决此问题。

To date, I’m having same issue when importing from Crypto.Cipher import AES even when I’ve installed/reinstalled pycrypto a few times. End up it’s because pip defaulted to python3.

~ pip --version
pip 18.0 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

installing pycrypto with pip2 should solve this issue.


回答 17

对于Windows 7:

我遇到了这个错误“模块错误Crypo.Cipher导入AES”

要在Windows中安装Pycrypto,

在命令提示符中尝试此操作,

设置路径= C:\ Python27 \ Scripts(即easy_install所在的路径)

然后执行以下命令

easy_install pycrypto

对于Ubuntu:

试试这个,

从“ https://pypi.python.org/pypi/pycrypto下载Pycrypto

然后使用终端将当前路径更改为下载路径:

例如:root @ xyz-virtual-machine:〜/ pycrypto-2.6.1#

然后使用终端执行以下命令:

python setup.py安装

对我有用。希望为所有人服务。

For Windows 7:

I got through this error “Module error Crypo.Cipher import AES”

To install Pycrypto in Windows,

Try this in Command Prompt,

Set path=C:\Python27\Scripts (i.e path where easy_install is located)

Then execute the following,

easy_install pycrypto

For Ubuntu:

Try this,

Download Pycrypto from “https://pypi.python.org/pypi/pycrypto

Then change your current path to downloaded path using your terminal:

Eg: root@xyz-virtual-machine:~/pycrypto-2.6.1#

Then execute the following using the terminal:

python setup.py install

It’s worked for me. Hope works for all..


回答 18

可以通过安装C ++编译器(python27或python26)来解决此问题。从Microsoft https://www.microsoft.com/zh-cn/download/details.aspx?id=44266下载它,然后重新运行命令:pip install pycrypto当您杀死.NET 进程时运行gui Web访问easy_install.exe

This problem can be fixed by installing the C++ compiler (python27 or python26). Download it from Microsoft https://www.microsoft.com/en-us/download/details.aspx?id=44266 and re-run the command : pip install pycrypto to run the gui web access when you kill the process of easy_install.exe.


回答 19

也许您应该这样做:pycryptodome == 3.6.1将其添加到requirements.txt并安装,这应该消除错误报告。这个对我有用!

Maybe you should this: pycryptodome==3.6.1 add it to requirements.txt and install, which should eliminate the error report. it works for me!


回答 20

这对我有用

pip install pycryptodome==3.4.3

This worked for me

pip install pycryptodome==3.4.3

回答 21

嗯,这可能会出现奇怪,但安装后pycrypto还是pycryptodome,我们需要更新的目录名cryptoCryptolib/site-packages

参考

Well this might appear weird but after installing pycrypto or pycryptodome , we need to update the directory name crypto to Crypto in lib/site-packages

Reference


回答 22

我是3.7。在我尝试安装加密货币后,问题仍然存在。在我的情况下,pycrypto只是失败了。所以最后我的构建通过下面的包传递:pip install pycryptodome

I’m with 3.7. The issue remains after I try to install crypto. And pycrypto just fails in my case. So in the end my build passed via package below: pip install pycryptodome