204

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.当我尝试运行 app.py(Python 3.3、PyCrypto 2.6)时,我的 virtualenv 不断返回上面列出的错误。我的导入语句只是 from Crypto.Cipher import AES 。我寻找重复项,你可能会说有一些,但我尝试了解决方案(尽管大多数甚至都不是解决方案),但没有任何效果。

You can see what the files are like for PyCrypto below:您可以在下面看到 PyCrypto 的文件是什么样的:

  • 5
    Looks like you're in a virtualenv. Is your virtualenv activated properly?看来您处于 virtualenv 中。你的 virtualenv 是否正确激活? CommentedOct 27, 2013 at 20:44
  • @ChristianTernus It is activated properly.@ChristianTernus 它已正确激活。 CommentedOct 27, 2013 at 23:41
  • I had the same issue. This will sound a bit lame but to resolve it just I stopped using OSX, I installed VirtualBox and a linux distro and it all worked :)我有同样的问题。这听起来有点蹩脚,但为了解决这个问题,我停止使用 OSX,安装了 VirtualBox 和 Linux 发行版,一切都正常了:)
    – Jordan
     CommentedMay 28, 2016 at 0:30
  • 2
    Wait what? ImportError is shown in python 2 while in python 3 it is ModuleNotFoundError. 
    – Nouman
     CommentedAug 13, 2018 at 14:30等等什么? ImportError 显示在 python 2 中,而在 python 3 中显示 ModuleNotFoundError 。
  • @BlackThunder, python3 -c 'import foo' yields ImportError: No module named 'foo'. Did I miss something? 
    – r2evans
     CommentedJul 2, 2019 at 16:28@BlackThunder, python3 -c 'import foo' 产生 ImportError: No module named 'foo' 。我错过了什么吗?

36 Answers  36 个答案 

231

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 安装时,我在 Mac 上遇到了同样的问题。然后我删除了 pycrypto 并使用 easy_install 再次安装它,如下所示:

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)也正如卢克评论的那样:如果您在运行这些命令时遇到问题,请务必以管理员身份运行它们(sudo)

As winklerr notes in their answer, pycrypto is no longer safe. Use pycryptodome instead, it is a drop-in replacement正如 winklerr 在他们的回答中指出的那样,pycrypto 不再安全。使用 pycryptodome 代替,它是一个直接替代品

  • 6
    It's odd, but this fixed the issue on OS X Mavericks for me. I needed sudo for system wide libs.这很奇怪,但这为我解决了 OS X Mavericks 上的问题。我需要 sudo 来获取系统范围的库。
    – Paul
     CommentedMay 19, 2014 at 9:09 
  • 2
    thanks elad, i'm using 10.9.3 and i got same error. thanks a million谢谢elad,我正在使用 10.9.3,但遇到了同样的错误。谢谢一百万
    – user1173426 用户1173426 
     CommentedJun 2, 2014 at 19:32
  • 4
    I had installed it with pip, which didn't work, then easy_install, which also didn't work. (This is on Windows.) Simply uninstalling it with pip as you suggested somehow made it work. I would have never thought to do that in a million years. Thank you.我用 pip 安装了它,但它不起作用,然后是 easy_install,它也不起作用。 (这是在 Windows 上。)只需按照您的建议用 pip 卸载它就可以了。我从来没想过在一百万年内会这么做。谢谢。 CommentedAug 23, 2015 at 23:04 
  • 6
    This still saves the day on El Capitan.这仍然挽救了 El Capitan 的局面。 CommentedFeb 28, 2016 at 5:28
  • 3
    Can anyone explain why this works? (on OSX 10.11.4 with python from homebrew)谁能解释为什么这有效? (在 OSX 10.11.4 上,使用自制程序中的 python) CommentedJun 2, 2016 at 18:47
189

WARNING: Don't use crypto or pycrypto anymore!警告:不要再使用 crypto 或 pycrypto

As you can read on this page, the usage of pycrypto is not safe anymore:正如您在此页面上所读到的,pycrypto 的使用不再安全:

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 容易受到 block_tempplace.c 中 ALGnew 函数中基于堆的缓冲区溢出的影响。它允许远程攻击者在Python应用程序中执行任意代码。它被分配了 CVE-2013-7459 编号。

Pycrypto didn’t release any fix to that vulnerability and no commit was made to the project since Jun 20, 2014.自 2014 年 6 月 20 日以来,Pycrypto 没有发布针对该漏洞的任何修复程序,也没有对该项目进行任何提交。

Update 2021-01-18 更新2021-01-18 

The CVE is fixed now (thanks @SumitBadsara for pointing it out!). You can find the current status of the open security tickets for each package at the Debian security tracker:CVE 现已修复(感谢 @SumitBadsara 指出!)。您可以在 Debian 安全跟踪器中找到每个软件包的开放安全票证的当前状态:

Use Python3's pycryptodome instead!使用Python3的pycryptodome来代替!

Make sure to uninstall all versions of crypto and pycrypto first, then install pycryptodome:确保先卸载 crypto 和 pycrypto 的所有版本,然后安装 pycryptodome :

pip3 uninstall crypto 
pip3 uninstall pycrypto 
pip3 install pycryptodome

All of these three packages get installed to the same folder, named Crypto. Installing different packages under the same folder name can be a common source for errors!所有这三个包都安装到同一个文件夹中,名为 Crypto 。在同一文件夹名称下安装不同的软件包可能是错误的常见来源!

For more information, see pycryptodome.org.有关更多信息,请参阅 pycryptodome.org 。

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.为了避免不同版本的 pip 包或安装在同一文件夹下的包(即 pycrypto 和 pycryptodome )出现问题,您可以使用所谓的虚拟环境。在那里,可以为每个项目单独管理已安装的 pip 包。

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 docs.python-guide.org.有关更多信息,请参阅 docs.python-guide.org 。

132

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.我在 Mac 上也遇到了这个问题,这似乎与通过 pip 与 pycrypto 一起安装了一个不幸的类似名称的“crypto”模块(不确定它的用途)有关。

The fix seems to be removing both crypto and pycrypto with pip:该修复似乎是使用 pip 删除 crypto 和 pycrypto:

sudo pip uninstall crypto
sudo pip uninstall pycrypto

and reinstalling pycrypto: 并重新安装 pycrypto: 

sudo pip install pycrypto

Now it works as expected when I do something like:现在,当我执行以下操作时,它会按预期工作:

from Crypto.Cipher import AES
74

I had the same problem (though on Linux). The solution was quite simple - add:我也遇到了同样的问题(尽管是在 Linux 上)。解决方案非常简单 - 添加:

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.到我的 app.yaml 文件。由于这在过去工作正常,我认为这是一个新要求。

  • 1
    Just to add to this a little more, in my case this wasn't working as I had multiple versions of Python interpreters on my machine and I was installing the libraries in different versions. What I did was moved into the virtual environment and it worked as smooth as silk.补充一点,就我而言,这不起作用,因为我的机器上有多个版本的 Python 解释器,并且我正在安装不同版本的库。我所做的事情被转移到虚拟环境中,并且工作起来像丝绸一样顺利。
    – neaGaze
     CommentedAug 5, 2017 at 15:30
  • @neaGaze How did you "move into the virtual environment" to get it to work "smooth as silk"?@neaGaze 你是如何“进入虚拟环境”并使其“如丝般顺畅”工作的? CommentedFeb 3, 2018 at 1:45
  • 1
    @Praxiteles Well it's been a while since I did that project but I think I used the Python virtual env setting. In Python you can either use a standalone version or the virtual environment. You can check out this link if you need more information pythonforbeginners.com/basics/how-to-use-python-virtualenv 
    – neaGaze
     CommentedFeb 4, 2018 at 15:39@Praxiteles 自从我做那个项目以来已经有一段时间了,但我想我使用了 Python 虚拟环境设置。在 Python 中,您可以使用独立版本或虚拟环境。如果您需要更多信息,可以查看此链接 pythonforbeginners.com/basics/how-to-use-python-virtualenv
  • 16
    This answer does not explain everything, where are you getting app.yaml file?  CommentedJun 12, 2019 at 3:34这个答案并不能解释一切,你从哪里得到 app.yaml 文件?
  • 4
    You shouldn't use pycrypto no more! Check my answer for a better solution!  CommentedSep 25, 2019 at 8:19您不应该再使用 pycrypto 了!检查我的答案以获得更好的解决方案!
50

I found the solution. Issue is probably in case sensitivity (on Windows).我找到了解决方案。 问题可能是区分大小写(在 Windows 上)。

Just change the name of the folder:只需更改文件夹的名称:

C:\Python27\Lib\site-packages\crypto

To this: 对此: 

C:\Python27\Lib\site-packages\Crypto

This is how folder was named after installation of pycrypto:这是安装 pycrypto 后文件夹的命名方式:enter image description here

I've changed it to: 我已将其更改为:  enter image description here

And now the following code works fine:现在以下代码可以正常工作:enter image description here

  • This works, but there are too many of packages inside all need to be renamed.这可行,但是里面有太多包都需要重命名。
    – Alex
     CommentedJan 18, 2019 at 19:24
  • @user1288329 Why would that be the case? Other packages are imported lowercase. Only renaming as Crypto worked for me.@user1288329 为什么会这样呢?其他包都是小写导入的。只有重命名为 Crypto 对我有用。
    – Burak
     CommentedDec 6, 2020 at 15:57
  • 1
    This problem probably occurs because crypto was installed before, which created the directory lowercase. And case insensitive Windows put all things in the same directory afterwards. 
    – Burak
     CommentedDec 6, 2020 at 15:58出现此问题的原因可能是之前安装了crypto,从而创建了小写目录。并且不区分大小写的 Windows 之后将所有内容都放在同一目录中。
  • 3
    I have run into this problem so many times and have never solved it. You, my good sir, are both a gentleman and a scholar. <hat tip>我已经多次遇到这个问题并且从未解决过。您,我的好先生,既是一位绅士,又是一位学者。 <帽子提示> CommentedJan 20, 2021 at 14:31
  • the uppercase crypto will change to lowercase automatically sometimes, it it possible to fixed is forever?大写的密码有时会自动变成小写,有可能永远固定吗?
    – Dolphin
     CommentedNov 17, 2023 at 7:56
42

On the mac... if you run into this.. try to see if you can import crypto instead?在 Mac 上......如果你遇到这个......尝试看看是否可以导入加密货币?

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.如果是这样..包名称是问题 C 与 c 。要解决这个问题...只需将这些行添加到脚本的顶部即可。

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

You know should be able to import paramiko successfully.您知道应该能够成功导入 paramiko。

29

Uninstalling crypto and pycrypto works on me. Then install only pycrypto:卸载 crypto 和 pycrypto 对我有用。然后仅安装 pycrypto :

pip uninstall crypto 
pip uninstall pycrypto 
pip install pycrypto
26

type command: 输入命令: 

sudo pip install pycrypto
17

If you are in a Mac OS, rename the lib folder:如果您使用的是 Mac OS,请重命名 lib 文件夹:

lib/python3.7/site-packages/crypto

To this: 对此: 

lib/python3.7/site-packages/Crypto
11

Run the following codes in your terminal screen在终端屏幕中运行以下代码

pip uninstall crypto
pip uninstall pycryptodome
pip install pycryptodome
  • is this library issue with different versions of python? Seemed to work fine for python 2这个库是不同版本的 python 的问题吗?似乎对 python 2 工作得很好 CommentedMay 18, 2022 at 9:04
  • 2
    Problem with possible python versions. Please use one of the current python versions可能的 python 版本有问题。请使用当前的 python 版本之一
    – isa
     CommentedMay 18, 2022 at 12:16
8

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...自从在 OSX 10.8.5 (Mountain Lion) 上使用 GoogleAppEngineLauncher(版本 > 1.8.X)和 GAE Boilerplate 以来,我遇到了同样的问题 'ImportError: No module named Crypto.Cipher' 。在具有 python 2.7 运行时的 Google App Engine SDK 中,建议使用 pyCrypto 2.6 版本。对我有用的解决方案是......

1) Download pycrypto2.6 source extract it somewhere(~/Downloads/pycrypto26)1)下载pycrypto2.6源代码并将其解压到某处(~/Downloads/pycrypto26

e.g., git clone https://github.com/dlitz/pycrypto.git例如,git 克隆 https://github.com/dlitz/pycrypto.git

2) cd (cd ~/Downloads/pycrypto26) then2) cd (cd ~/Downloads/pycrypto26 ) 然后

3) Execute the following terminal command inside the previous folder in order to install pyCrypto 2.6 manually in GAE folder.3) 在上一个文件夹中执行以下终端命令,以便在 GAE 文件夹中手动安装 pyCrypto 2.6。

sudo python setup.py install --install-lib /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine
  • 1
    the OP's question has to do with Windows and not OSXOP的问题与Windows有关,而不是OSX
    – galdin
     CommentedSep 20, 2014 at 13:54
  • It might be useful to note that installing to the project root will also work (regardless of platform)注意到安装到项目根目录也可以工作(无论平台如何)可能会很有用
    – ocodo
     CommentedNov 18, 2014 at 23:59
  • It's great answer for yosemite user!对于优胜美地用户来说,这是一个很好的答案! CommentedJan 16, 2015 at 12:39
  • @Lazaros Dinakis : This is the solution that worked.. of all the solutions mentioned in this page. Thanks@Lazaros Dinakis:这是本页提到的所有解决方案中有效的解决方案。谢谢 CommentedApr 23, 2015 at 11:48
  • this answer solved it for me on mac OSX Yosemite 10.10.5. Only thing is the installation failed with errors for me. so i manually copied the Crypto lib from my python lib into the google_appengine lib. and now it works.这个答案在 mac OSX Yosemite 10.10.5 上为我解决了这个问题。唯一的问题是安装失败并出现错误。所以我手动将 Crypto lib 从 python lib 复制到 google_appengine lib 中。现在可以了。 CommentedNov 10, 2015 at 14:17 
8

If you are using RedHat or a RedHat-based distro like Fedora or CentOS, you can install it with the following command:如果您使用的是 RedHat 或基于 RedHat 的发行版(例如 Fedora 或 CentOS),则可以使用以下命令安装它:

sudo yum install pycrypto

In my case, I was not able to install it using pip.就我而言,我无法使用 pip 安装它。

6

If you are using this module with Python3 and having trouble with import. try this.如果您在 Python3 中使用此模块并且在导入时遇到问题。试试这个。

pip uninstall crypto
pip uninstall pycryptodome
pip install pycryptodome

Good Luck! 祝你好运! 

6

Even after installing Crypto I was getting below error:即使安装了 Crypto 之后,我仍然收到以下错误:

python my_script.py
Traceback (most recent call last):
  File "D:\gitworkspace\cloudtools\py\my_script.py", line 19, in <module>
    from Crypto.Cipher import AES
ModuleNotFoundError: No module named 'Crypto'

Then in this post I found the command that solved the issue.然后在这篇文章中我找到了解决该问题的命令。

pip install pycryptodome
5

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好吧,这可能看起来很奇怪,但是在安装 pycrypto 或 pycryptodome 后,我们需要将 中的目录名称 crypto 更新为 Crypto 

Reference 参考 

4

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这可能是加载通过 pip 安装的 python 模块的问题。请参阅此答案 Can't load Python module displayed by pip from site-packages directory 并尝试类似的操作

python -m pip install pycrypto
4

Worked for me (Ubuntu 17.10)为我工作(Ubuntu 17.10)

Removing venv and creating it again with python v3.6删除 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 is deprecated, had problems with it, used PycryptodomePycrypto 已弃用,遇到问题,使用 Pycryptodome

4

This worked for me  这对我有用 

pip install pycryptodome==3.4.3
4

Step-1: You are suggested to uninstall those libraries if it is installed on your machine步骤 1:如果您的计算机上安装了这些库,建议您uninstall

pip uninstall pycrypto
pip uninstall crypto 

Step-2: Now, install the pycryptodome instead of pycrypto or crypto. Hope, it will solve your problem.步骤 2:现在,安装 pycryptodome 而不是 pycrypto 或 crypto 。希望它能解决您的问题。

pip install pycryptodome

Step-3: As you need, you can import the relevant libraries.Step-3:根据需要导入相关库即可。

from Crypto.Cipher import AES
from Crypto import Random
from Crypto.Random import get_random_bytes
3

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''.我通过将第一个字母大小写更改为大写来解决这个问题。确保“from Crypto.Cipher import AES”而不是“from crypto.Cipher import AES”。

3

Try with pip3:尝试使用 pip3 :

sudo pip3 install pycrypto
2

For CentOS 7.4 I first installed pip and then pycrypto using pip:对于 CentOS 7.4,我首先安装了 pip,然后使用 pip 安装了 pycrypto:

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

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.到目前为止,即使我已经安装/重新安装了 pycrypto 几次,我在导入 from Crypto.Cipher import AES 时也遇到了同样的问题。最终是因为pip默认为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.使用 pip2 安装 pycrypto 应该可以解决这个问题。

2

One more reminder if you still encounter this issue after uninstall crypto and pycrypto like this如果您在卸载 crypto 和 pycrypto 后仍然遇到此问题,请再次提醒您

pip3 uninstall crypto
pip3 uninstall pycrypto

Just check if there is a directory named crypto(lower case) in your site-packages under /usr/local/lib/python3.9/site-packages, make sure the python version your used and the right site-packages path, then remove the crypto directory, the try to install again.只需检查 /usr/local/lib/python3.9/site-packages 下的 site-packages 中是否有一个名为 crypto (小写)的目录,确保您使用的 python 版本和正确的 site-packages 路径,然后删除加密目录,然后尝试再次安装。

2

this works for me: 这对我有用: 

pip install pycryptodomex

then 然后 

from Cryptodome.Cipher import AES
  • Your answer could be improved with additional supporting information. Please通过额外的支持信息可以改进您的答案。请edit 编辑  to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers添加更多详细信息,例如引文或文档,以便其他人可以确认您的答案是正确的。您可以找到有关如何写出好的答案的更多信息in the help center 在帮助中心 .   
    – CommunityBot 机器人 
     CommentedJan 9, 2023 at 14:31
1

For Windows 7: 对于 Windows 7: 

I got through this error "Module error Crypo.Cipher import AES"我遇到了这个错误“模块错误 Crypo.Cipher import AES”

To install Pycrypto in Windows,要在 Windows 中安装 Pycrypto,

Try this in Command Prompt,在命令提示符中尝试这个,

Set path=C:\Python27\Scripts (i.e path where easy_install is located)设置path=C:\Python27\Scripts(即easy_install所在路径)

Then execute the following, 然后执行以下命令, 

easy_install pycrypto easy_install pycrypto 

For Ubuntu: 对于Ubuntu: 

Try this, 试试这个, 

Download Pycrypto from "https://pypi.python.org/pypi/pycrypto"从“ https://pypi.python.org/pypi/pycrypto ”下载 Pycrypto

Then change your current path to downloaded path using your terminal:然后使用终端将当前路径更改为下载路径:

Eg: root@xyz-virtual-machine:~/pycrypto-2.6.1#例如:root@xyz-虚拟机:~/pycrypto-2.6.1#

Then execute the following using the terminal:然后使用终端执行以下命令:

python setup.py install python setup.py 安装 

It's worked for me. Hope works for all..这对我有用。希望对所有人都有效..

1

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.这个问题可以通过安装C++编译器(python27或python26)来解决。从 Microsoft 下载 https://www.microsoft.com/en-us/download/details.aspx?id=44266 并重新运行命令: pip install pycrypto 以在杀死时运行 gui web 访问easy_install.exe 的过程。

1

Simply delete your existing folder of pycrypto/pycryptodome-3.11.0 located at:只需删除位于以下位置的现有文件夹 pycrypto/pycryptodome-3.11.0

..\Python\Python310\Lib\site-packages

Then run: 然后运行: 

pip install pycryptodome
1

Maybe you should add this to your requirements.txt file:也许您应该将其添加到您的 requirements.txt 文件中:

pycryptodome==3.6.1

Then install all dependencies using:然后使用以下命令安装所有依赖项:

pip install -r requirements.txt

This should eliminate the error report. It worked for me!这应该消除错误报告。这对我有用!

1

I'm with Python 3.7. The issue remains after I try to install crypto.我使用的是Python 3.7。我尝试安装 crypto 后问题仍然存在。

And pycrypto just fails in my case. So in the end my build passed by using the following package:而 pycrypto 在我的例子中失败了。所以最终我的构建通过使用以下包:

pip install pycryptodome

Not the answer you're looking for? Browse other questions tagged  or ask your own question.


来自  https://stackoverflow.com/questions/19623267/importerror-no-module-named-crypto-cipher