PyInstaller not correctly importing pycrypto… sometimes

五迷三道 提交于 2019-12-04 17:59:31

I was able to solve the problem with hithwen's recipe, but with a slightly different .spec file. I'll leave it here for reference for everyone.

# -*- mode: python -*-

#Tweaks to properly import pyCrypto

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

#Analysis remains untouched
a = Analysis(['myapp.py'],
             pathex=[],
             hiddenimports=[],
             hookspath=None,
             runtime_hooks=None)
#Add to the tree the pyCrypto folder
dict_tree = Tree(get_crypto_path(), prefix='Crypto', excludes=["*.pyc"])
a.datas += dict_tree
#As we have the so/pyd in the pyCrypto folder, we don't need them anymore, so we take them out from the executable path
a.binaries = filter(lambda x: 'Crypto' not in x[0], a.binaries)
#PYZ remains untouched
pyz = PYZ(a.pure)
#EXE remains untouched
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='myapp',
          debug=False,
          strip=None,
          upx=True,
          console=True )
#COLLECT remains untouched
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=None,
               upx=True,
               name='myapp')

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

I got it working by replacing pycrypto / pycryptodome with pycryptodomex. Sharing a link to the already posted answer: https://stackoverflow.com/a/50009769/4355695

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!