5

We've recently added pycrypto to a project we've been working on and now I am unable to run the software after it is built with pyinstaller.

I have had issues with new packages in pyinstaller, but I am unable to fix this one in particular.

The errors I've gotten when trying to run the built software are as follows (sorry for the paraphrasing, it takes quite a while to build)

At first it was something like:

No package Crypto.Cipher

So I added 'Crypto' to the hiddenimports in my .spec file. Then I got,

No module named Cipher

So I changed 'Crypto' to 'Crypto.Cipher' and then I got,

Crypto.Cipher has no attribute AES

So I changed 'Crypto.Cipher' to 'Crypto.Cipher.AES' then I got

File "C:\Folder\made\by\pyinstaller\Crypto.Cipher.AES", line 49 in <module>
ImportError: cannot import name blockalgo

So I changed 'Crypto.Cipher.AES' to 'Crypto.Cipher.AES.blockalgo' and the error didn't change.

I've tried a few different configurations, but the output of the build script always says something along the lines of

ERROR: Hidden import 'blockalgo' not found.

Does anybody know how to get this to import correctly, or know a trick to get pycrypto to play nice with pyinstaller?

horriblyUnpythonic
  • 853
  • 2
  • 14
  • 34
  • 2
    Pycrypto is [unmaintained and has known vulnerabilities](https://github.com/dlitz/pycrypto/issues/173). Use [`pycryptodome`](https://pycryptodome.readthedocs.io/en/latest/), it is a drop-in replacement. – Merlijn Sebrechts Aug 05 '16 at 08:56
  • I tried with pycryptodome installed. It's erroring out for want of a `OSError: Cannot load native module 'Crypto.Hash._MD5'`. I included that in hiddenimports.. it's creating a `Crypto.Hash._MD5.pyd` file in the build but still no dice.. program crashes. And it looks like its looking in all the wrong places. – Nikhil VJ Apr 24 '18 at 19:12
  • Saw a recco to use pycryptodomex instead : https://stackoverflow.com/a/48408950/4355695 – Nikhil VJ Apr 24 '18 at 19:13

4 Answers4

3

According to the pyinstaller manual:

You can verify that hidden import is the problem by using Python's verbose imports flag. If the import messages say "module not found", but the warnproject.txt file has no "no module named..." message for the same module, then the problem is a hidden import.

Hidden imports are handled by hooking the module (the one doing the hidden imports) at Analysis time. Do this as follows:

  1. Create a file named hook-module.py (where module is the fully-qualified Python name, eg, hook-xml.dom.py) and place it somewhere. Remember the place as your private hooks directory.

  2. In the .spec file, pass your private hooks directory as hookspath argument to Analysis so will be searched. Example:

    a = Analysis(['myscript.py'], hookspath='/my/priv/hooks') In most cases the hook module will have only one line:

    hiddenimports = ['module1', 'module2'] When the Analysis finds this file, it will proceed exactly as though the module explicitly imported module1 and module2.

This question seems related, the answers might also be useful for you.

Finally, this report seems to contain a similar problem. The user seemingly was able to fix it by updating to pyinstaller 2.1, so you might want to give that a try if you haven't already.

Community
  • 1
  • 1
kadu
  • 746
  • 12
  • 29
3

This Answer :

From https://stackoverflow.com/a/48408950/4355695 : Use pycryptodomex instead of pycryptodome. And @galgalesh's comment below the OP's question gave why pycrypto should no longer to be used.

pip uninstall -y pycrypto pip uninstall -y pycryptodome pip install pycryptodomex

pycryptodomex gives a clearly disambiguous Cryptodome module to replace Crypto. So, in your .py programs, replace Crypto with Cryptodome:

from Cryptodome.PublicKey import RSA

I now ran pyinstaller afresh and it worked out properly. No need to do any special hiddenimports etc. In the dist folder, there's now a clear Crpytodome folder holding all the .pyd's.

Works perfectly for me !

Use pycryptodomex instead of pycrypto and it would work !

I think it's due to python 3.6 and major evolutions of pycrypto to work with ! Then it stop working with 2.7.16 !

HSMKU
  • 81
  • 10
  • I found a better solution, you can download this version : pycrypto-2.6.win32-py2.7.exe ! Then for compiling, use UPX packer above pyinstaller and the library will build successfully ! – HSMKU Mar 27 '19 at 13:31
  • Thanks you,you gave me the right solution but not quite. I had to uninstall also pycryptodomex but install nothing else, only uninstall pycrypto, pycryptodome and pycryptodomex solving my problem. Don't ask me why, i don't know ... perhaps some library conflict – tomboul Mar 06 '23 at 14:41
1

Change? Why not add? Adding these to hiddenimport solved this issue: 'Crypto', 'Crypto.Cipher', 'Crypto.Cipher.AES', 'Crypto.Random',

swdev
  • 4,997
  • 8
  • 64
  • 106
1

From https://stackoverflow.com/a/48408950/4355695 : Use pycryptodomex instead of pycryptodome. And @galgalesh's comment below the OP's question gave why pycrypto should no longer to be used.

pip uninstall -y pycrypto
pip uninstall -y pycryptodome
pip install pycryptodomex

pycryptodomex gives a clearly disambiguous Cryptodome module to replace Crypto. So, in your .py programs, replace Crypto with Cryptodome:

from Cryptodome.PublicKey import RSA

I now ran pyinstaller afresh and it worked out properly. No need to do any special hiddenimports etc. In the dist folder, there's now a clear Crpytodome folder holding all the .pyd's.

Nikhil VJ
  • 5,630
  • 7
  • 34
  • 55