我正在不同的 ubuntu 机器上使用 PyInstaller 打包一个项目。 在其中一些上,执行生成的项目时,会抛出以下错误:
File "~/PyInstaller-2.1/proj/build/proj/out00-PYZ.pyz/Crypto.Random", line 28, in ImportError: cannot import name OSRNG
但是导入在 python 控制台中工作得很好,我可以执行该项目而不打包它。
我尝试卸载并重新安装 pycrypto 但没有成功,我还尝试添加特定
from Crypto.Random import OSRNG
到主文件,以便 PyInstaller 拾取它。
最佳答案
我能够使用 hithwen 的配方解决问题,但使用稍微不同的 .spec
文件。我就放在这里,供大家引用。
# -*- 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')
关于python - PyInstaller 有时无法正确导入 pycrypto...,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23289047/