gpt4 book ai didi

python - PyInstaller 没有正确导入 pycrypto ... 有时

转载 作者:行者123 更新时间:2023-11-28 22:49:52 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com