Execute python script in console via bash script with double-click
Question:
I have a python script, that runs fine if i run in on the console with python3 script.py
. For some reason, the script should be executed via a bash-script "start.sh". A MWE would be
#!/bin/bash
python3 GUIpyPCS.py
When I run the script by double-clicking it, it doesent open a terminal window. Hence, the python script is executed invisibly in the background and not on a new console window.
How can I ensure, that the python script is executed on a console by double-clicking the bash-script? Is there something I can add to the bash script? I am currently testing on Kubuntu 20.04, but the solution should also applicable to other distributions/window managers.
Answers:
———- Assumptions ———-
- the script is in a directory
- you open that directory in the graphics interface (Nemo, Nautilus, any other file manager)
- you double-click the icon representing the script
- you expect to see a terminal window open, with the script execution in it
———- In your script do ———-
- start a terminal window
- that terminal command includes a call to the script
- and lastly a call to the
read
command, to keep the window opened until you are done
- you press enter to close the window
———- Ex ———-
#!/bin/bash
#
xterm -e "/bin/ls /etc; read" &
———- Details ———-
-
the -e
option to xterm
is the command that will be executed in the new window
-
I put /bin/ls /etc; read
instead of your script. You would put your_script.bash arg1 arg2; read
-
you could do something similar with gnome-terminal
instead of xterm
-
the read
is not essential, it could be a sleep 5
for example. Or if your script already has a delay or something, you might not need anything.
-
I used xterm
since it is available on all distributions, which ever window manager you use.
1) Create 0.sh file in (project folder) with your script. Then edit It under text editor and put to It:
#!/bin/bash
#
pyton3 0.py
Save the 0.sh file.
2) Right Click on 0.sh file.
3) Permissions Tab / Set As Runnable.
4) Righi Click on 0.sh file / Open with other Application / Use as Custom Command:
type: bash
set mark: Use as Default…
press: ok …
5) 0.sh file must be in the Project Folder.
6) Double Click on 0.sh file.
I have a python script, that runs fine if i run in on the console with python3 script.py
. For some reason, the script should be executed via a bash-script "start.sh". A MWE would be
#!/bin/bash
python3 GUIpyPCS.py
When I run the script by double-clicking it, it doesent open a terminal window. Hence, the python script is executed invisibly in the background and not on a new console window.
How can I ensure, that the python script is executed on a console by double-clicking the bash-script? Is there something I can add to the bash script? I am currently testing on Kubuntu 20.04, but the solution should also applicable to other distributions/window managers.
———- Assumptions ———-
- the script is in a directory
- you open that directory in the graphics interface (Nemo, Nautilus, any other file manager)
- you double-click the icon representing the script
- you expect to see a terminal window open, with the script execution in it
———- In your script do ———-
- start a terminal window
- that terminal command includes a call to the script
- and lastly a call to the
read
command, to keep the window opened until you are done - you press enter to close the window
———- Ex ———-
#!/bin/bash
#
xterm -e "/bin/ls /etc; read" &
———- Details ———-
-
the
-e
option toxterm
is the command that will be executed in the new window -
I put
/bin/ls /etc; read
instead of your script. You would putyour_script.bash arg1 arg2; read
-
you could do something similar with
gnome-terminal
instead ofxterm
-
the
read
is not essential, it could be asleep 5
for example. Or if your script already has a delay or something, you might not need anything. -
I used
xterm
since it is available on all distributions, which ever window manager you use.
1) Create 0.sh file in (project folder) with your script. Then edit It under text editor and put to It:
#!/bin/bash
#
pyton3 0.py
Save the 0.sh file.
2) Right Click on 0.sh file.
3) Permissions Tab / Set As Runnable.
4) Righi Click on 0.sh file / Open with other Application / Use as Custom Command:
type: bash
set mark: Use as Default…
press: ok …
5) 0.sh file must be in the Project Folder.
6) Double Click on 0.sh file.