简体   繁体   English

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

[英]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.

相关问题 如何使用 pyInstaller 将 python 脚本转换为可执行文件 - how to convert python script to executable with pyInstaller 如何允许 python 脚本复制使用 pyinstaller 创建的可执行文件? - How to allow python script to replicate the executable file created with pyinstaller? 将 python 文件转换为可执行文件时出现 Pyinstaller 脚本错误 - Pyinstaller script error when converting a python file into an executable 从使用 pyinstaller 运行单独的 python 脚本的 python 脚本创建 .exe - Creating a .exe from a python script which runs a separate python script using pyinstaller 将 python 脚本转换为 Windows 的可执行文件或图标? - Convert python script to an executable file or icon for Windows? Python脚本可执行文件与py2exe或pyinstaller - Python script executable with py2exe or pyinstaller Python脚本在bash中运行,但不在cron中运行? - Python script runs in bash, but not in cron? 与直接执行时不同的运行bash脚本(运行python脚本)的Cron作业 - Cron job that runs bash script (which runs python script) behaving different than when executed directly 无法使用Pyinstaller将Python脚本转换为可执行文件:找不到dist文件夹和PyQt5 - Cannot convert Python script into an executable using Pyinstaller: Empty dist folder & PyQt5 not found 将bash脚本转换为python - Convert bash script to python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM