[英]Convert python script which runs a bash script to executable file with Pyinstaller
I want to convert a python script which runs a local bash script to executable file with Pyinstaller.我想使用 Pyinstaller 将运行本地 bash 脚本的 python 脚本转换为可执行文件。
My project structure is as following:我的项目结构如下:
Project/
|-- bash_script/
| |-- script.sh
|-- main.py
The main.py contains a line which runs the script locally: main.py 包含一行在本地运行脚本:
output = subprocess.check_output('./bash_script/script.sh', shell=True).decode()
Now, after converting the main.py to executable file in linux, if I run it on a different location from where main.py is located, it won't find the script.现在,在将 main.py 转换为 linux 中的可执行文件后,如果我在与 main.py 所在的不同位置运行它,它将找不到脚本。
I want to add the shell script to the python executable file, so it won't depend on the script locally, but, I will just have the executable file and it will eventually run.我想将 shell 脚本添加到 python 可执行文件中,这样它就不会依赖于本地脚本,但是,我只需要可执行文件,它最终会运行。
I have tried using --add-data flag to pyinstaller converting commend, however it didn't work.我尝试使用 --add-data 标志来 pyinstaller 转换推荐,但它没有用。
Thanks!谢谢!
Note: I am using the following command:注意:我正在使用以下命令:
pyinstaller --add-data "./bash_script/script.sh:." --onefile main.py
and I get an error after running in dist dir:在 dist 目录中运行后出现错误:
/bin/sh: 1: ./bash_script/script.sh: not found
In your main.py
:在你的
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)
Then run:然后运行:
pyinstaller -F --add-data ./bash_script/script.sh:./bash_script main.py
And bobs your uncle!还有你叔叔鲍勃!
ps -F
is the same as --onefile
ps
-F
与--onefile
相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.