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.
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
来源:https://stackoverflow.com/questions/23289047/pyinstaller-not-correctly-importing-pycrypto-sometimes