繁体   English   中英

使用Pyinstaller将运行bash脚本的python脚本转换为可执行文件

[英]Convert python script which runs a bash script to executable file with Pyinstaller

我想使用 Pyinstaller 将运行本地 bash 脚本的 python 脚本转换为可执行文件。

我的项目结构如下:

Project/
|-- bash_script/
|   |-- script.sh
|-- main.py

main.py 包含一行在本地运行脚本:

output = subprocess.check_output('./bash_script/script.sh', shell=True).decode()

现在,在将 main.py 转换为 linux 中的可执行文件后,如果我在与 main.py 所在的不同位置运行它,它将找不到脚本。

我想将 shell 脚本添加到 python 可执行文件中,这样它就不会依赖于本地脚本,但是,我只需要可执行文件,它最终会运行。

我尝试使用 --add-data 标志来 pyinstaller 转换推荐,但它没有用。

谢谢!


注意:我正在使用以下命令:

pyinstaller --add-data "./bash_script/script.sh:." --onefile main.py

在 dist 目录中运行后出现错误:

/bin/sh: 1: ./bash_script/script.sh: not found

在你的main.py

import subprocess
import os

script = os.path.join(os.path.dirname(__file__),'bash_script','script.sh')
output = subprocess.check_output(script, shell=True).decode()
print(output)

然后运行:

pyinstaller -F --add-data ./bash_script/script.sh:./bash_script main.py

还有你叔叔鲍勃!

ps -F--onefile相同

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM