pyInstaller:将二进制可执行文件打包到项目的可执行文件中以运行

问题描述 投票:0回答:1

TLDR;

我想将

ffmpeg
可执行文件打包到我自己的可执行文件中。目前我正在得到

FileNotFoundError: [Errno 2] No such file or directory: 'ffmpeg'
Skipping ./testFile202312061352.mp4 due to FileNotFoundError: [Errno 2] No such file or directory: 'ffmpeg'

详情:

我正在使用以下命令创建可执行文件:

pyinstaller cli.py \
  --onefile \
  --add-binary /Users/<machineUser>/anaconda3/envs/my_env/bin/ffmpeg:bin

使用

ffmpeg
的代码不是我编写的。我想保持那部分不变。

当我在

conda
环境处于活动状态时从命令行运行时,我可以成功运行它,因为
python
(或者也许
anaconda
)知道二进制文件在哪里。我有一个非常空的
cli.py
。这似乎是入口点,我希望如果可能的话我可以在那里设置
bin
目录的路径...

我能够成功运行应用程序,如下所示:

(my_env) machineUser folder % "dist/cli_mac_001202312051431" ./testFile202312061352.mp4

我想像下面这样运行:

(base) machineUser folder % "dist/cli_mac_001202312051431" ./testFile202312061352.mp4

我想让我的可执行文件的 tmp 文件夹之外的世界保持不变。我不想更改在执行终止后将“留下”的东西。

问题:

有人可以提一下如何修改

pyinstaller
命令或在
cli.py
中更改哪些内容才能成功实现吗?

python ffmpeg anaconda pyinstaller anaconda3
1个回答
0
投票

您尝试过使用规范文件吗?至少使用这些文件我能够执行您想要实现的操作。 另请根据此处提供的信息检查下面的示例

https://pyinstaller.org/en/stable/spec-files.html#adding-files-to-the-bundle

https://pyinstaller.org/en/stable/runtime-information.html

最小示例

以下示例只是通过子进程调用 ffmpeg 并显示提供的帮助。所有提供的文件都是相互关联的。最后,要编译可执行文件,请运行

pyinstaller specfile.spec

main.py

import os
import sys
import subprocess

# check if run as script or exe --> https://pyinstaller.org/en/stable/runtime-information.html
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
    ROOT = sys._MEIPASS
else:
    ROOT = os.path.dirname(__file__)

# set path of ffmpeg...
ffmpeg = os.path.join(ROOT, "ffmpeg.exe")
# ... and run the tool
subprocess.run([ffmpeg, "-h"])

input("press any key to close the app")

specfile.spec

import os
import PyInstaller.config
import sys

# how to add data to pyinstaller exe: https://pyinstaller.org/en/stable/spec-files.html#adding-files-to-the-bundle
sys.setrecursionlimit(10000)

launch_script = "main.py"
exe_name = "main.exe"

# path and stuff
ROOT = os.path.dirname(PyInstaller.config.CONF["spec"])

a = Analysis([os.path.join(ROOT, launch_script)],
             # extend search path
             pathex=[ROOT],
             binaries=[],
             datas = [("ffmpeg.exe", ".")],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=None)
pyz = PYZ(a.pure, a.zipped_data,cipher=None)

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name=exe_name,
          debug=False,
          strip=False,
          upx=True,
          console=True)
© www.soinside.com 2019 - 2024. All rights reserved.