Pyinstaller does not correct pycripto import ... sometimes

I am building a project with pyinstaller on different ubuntu machines. In some of them, when executing a generated project, this error occurs:

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

However, the import works fine in the python console, and I can execute the project without packing it.

I tried uninstalling and reinstalling pycrypto without success, I also tried adding a separate

from Crypto.Random import OSRNG

to the main file, so pyinstaller will select it.

+2
source share
2 answers

I solved this by adding the Crypto directory tree to the spec file

I get the path using this function:

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

And then replace in the spec file:

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

I managed to solve the problem with the hithwen recipe, but with a slightly different .spec file. I will 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') 
+3
source

Source: https://habr.com/ru/post/983935/


All Articles