1

I want to convert a python script which runs a local bash script to executable file with Pyinstaller.

My project structure is as following:

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

The main.py contains a line which runs the script locally:

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.

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.

I have tried using --add-data flag to pyinstaller converting commend, however it didn't work.

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:

/bin/sh: 1: ./bash_script/script.sh: not found
9
  • According to serverfault.com/questions/319115/… you can pipe a bash script into bash via stdin. So you could embed the bash script as string in a Python script and feed it to bash. Commented Nov 27, 2022 at 8:49
  • @MichaelButscher I don't want to pipeline the result of the executable file, because eventually I want a single executable file which I can run from anywhere and still it will work. Commented Nov 27, 2022 at 8:54
  • I meant you should use "subprocess" to run bash in a process and feed the script into bash's stdin. This has nothing to do with the stdin or stdout of your executable. Commented Nov 27, 2022 at 9:51
  • Oh ok, but how can I do that? because I am calling the bash script inside what I am trying to convert to executable file. Can you maybe elaborate on that? thanks! Commented Nov 27, 2022 at 9:55
  • @MichaelButscher I am sorry, forgot to tag you. Commented Nov 27, 2022 at 10:11

1 Answer 1

1

In your 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!

p.s. -F is the same as --onefile

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.