python - PyInstaller 有时无法正确导入 pycrypto...

标签 python python-2.7 pyinstaller pycrypto

我正在不同的 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/

相关文章:

python - 如何让 Luigi 任务自行关闭?

python - 如何在 QuickFix 中获取 SessionID

python - Pyinstaller - 编译应用程序后找不到libmagic

python - 我如何通过我的规范文件说服 PyInstaller 在它生成的 EXE 中包含 libvlc.dll?

python - 如何在Django中实现这个模型?

python - Pandas - 如何在数据帧的每组中执行值与时间的 OLS 回归?

python - PyCharm 找不到 Keras

python - 比较两个 df 以发现缺失的行

python - 查找两个字符串之间的差异位置

python - python是否依赖于microsoft visual c++ redistributable?