'How to fix "ImportError: cannot import name _AES" from Crypto.Cipher

I'm using the pycrypto module to encrypt files. When running the python code, it works well:

$ python encrypt_file.py file

but when I build encrypt_file.py to a binary:

$ pyinstaller -F zip_disk.py

and run the binary under dist

$ ./encrypt_file file

it shows the following error:

File "<string>", line 24, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller-2.1-py2.7.egg/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/xxxx/zip_disk/build/zip_disk/out00-PYZ.pyz/Crypto.Cipher.AES", line 50, in <module>
ImportError: cannot import name _AES

Why does this happen?
How to fix ImportError?

  • My import statement is
    from Crypto.Cipher import AES
    
  • Python version: Python 2.7.6
  • PyCrypto version is 2.6.1. I have tried to install pycrypto from source and via pip, both result in the the same ImportError.
  • Platform: Linux Ubuntu 3.13.0-32-generic x86_64 x86_64 GNU/Linux


Solution 1:[1]

The pycrypto developers have stopped futher upgrades. Use instead pycryptodome which replaces pycrypto.

pip install pycryptodome

Now it works fine (did for me)!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gino Mempin