Search code examples
pythonpython-2.7pyinstallerpycrypto

PyInstaller not correctly importing pycrypto... sometimes


I'm packaging a project with PyInstaller on different ubuntu machines. On some of them, when executing the generated project, it throws this error:

File "~/PyInstaller-2.1/proj/build/proj/out00-PYZ.pyz/Crypto.Random", line 28, in ImportError: cannot import name OSRNG

However the import works perfectly ok in python console and I can execute the project without packaging it.

I've tried uninstalling and reinstalling pycrypto without success, I've also tried adding a specific

from Crypto.Random import OSRNG

to the main file just so PyInstaller would pick it up.


Solution

  • I've solved it by adding Crypto directory tree to spec file

    I get the path with this function:

    def get_crypto_path():
        '''Auto import sometimes fails on linux'''
        import Crypto
        crypto_path = Crypto.__path__[0]
        return crypto_path
    

    And then substitute in spec file:

    dict_tree = Tree('CRYPTO_PATH', prefix='Crypto', excludes=["*.pyc"])
    a.datas += dict_tree