如何将 UPX 与 pyinstaller 一起使用?

How do I use UPX with pyinstaller?

如何将 UPX 与 pyinstaller 一起使用?

我正在关注文档。

我已经下载了UPX

我的文件看起来像:

import csv
import selenium
import pandas

print('Hello')

然后我运行:

pyinstaller -F --upx-dir C:\Users\DD\Downloads\upx394w\upx394w3\upx308w\upx.exe zz.spec

这不会影响文件的大小。

知道如何让它工作吗?

# -*- mode: python -*-

block_cipher = None


a = Analysis(['zz.py'],
             pathex=['C:\Users\DA\13\14'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='zz',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

需要指定 UPX 目录,而不是 UPX 可执行文件:

例如:

pyinstaller myfile.py --upx-dir=..\upx391w -y --onefile

添加新答案,因为 PyInstaller 现在(2019 年 9 月)似乎比当前答案和评论建议的更有用。

如果 UPX 正常工作,我会在构建早期看到输出 934 INFO: UPX is available.

此外,我可以在 PyInstaller 调用 upx 的地方看到许多输出行。

我没有指定 --upx-dir,但 upx.exe 在我的 $PATH 环境变量中可用。

除了 GlennS 的评论:pyinstaller 文档中确切说明了此行为。所以在这方面这不是未记录的意外收益。
参见 https://pyinstaller.readthedocs.io/en/v3.3.1/usage.html#using-upx

只需安装upx

sudo apt install upx

Pyinstaller 默认搜索 upx 除非你指定标志--noupx.

以上安装会自动将upx添加到$PATH变量中,然后您不需要指定--upx-dir标志。

其他方法是从 upx's repo 下载 zip 文件并使用 --upx-dir 标志。
参考的回答。

For windows, the steps might differ.

检查您的脚本名称是否拼写正确。在我的例子中,我收到了上述错误,因为我使用的是 pyinstaller.exe -F .\my_script.py 但我的脚本名称是 my-script.py (注意“-”与“_”)。