# Launching a python program with the button

**URL:** <https://community.blokas.io/t/launching-a-python-program-with-the-button/3951>\
**Category:** Support\
**Created:** [September 5, 2022, 2:09pm UTC](https://community.blokas.io/t/launching-a-python-program-with-the-button/3951 "2022-09-05T14:09:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nburge](https://community.blokas.io/user_avatar/community.blokas.io/nburge/32/1774_2.png) [@nburge](https://community.blokas.io/u/nburge)\
**Post date:** [September 5, 2022, 2:09pm UTC](https://community.blokas.io/t/launching-a-python-program-with-the-button/3951/1 "2022-09-05T14:09:40Z")

</div>

Hello Pisound community, I’m trying to create a button script that will launch a python program. To run the program from the command line I have a short bash script: StartRig.sh that does the job as follows:

```auto
#!/bin/sh

/usr/bin/python3 Documents/python3_stuff/rig_FT232H.py

```

However when I tried to make a button script to launch that same bash script, the python program does not work. Please see my button script below…

```auto
#!/bin/sh

. /usr/local/pisound/scripts/common/common.sh

. /home/patch/StartRig.sh

flash_leds 100

```

---

<div class="post-metadata">

**Author:** ![Giedrius](https://community.blokas.io/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@Giedrius](https://community.blokas.io/u/Giedrius)\
**Post date:** [September 5, 2022, 4:28pm UTC](https://community.blokas.io/t/launching-a-python-program-with-the-button/3951/2 "2022-09-05T16:28:04Z")

</div>

> [@nburge](#):
>
> ```auto
> #!/bin/sh
> 
> /usr/bin/python3 Documents/python3_stuff/rig_FT232H.py
> 
> ```

This script depends on the current working directory (the location from which it’s executed)

If you change the relative path `Documents/python3_stuff/rig_FT232H.py` into an absolute path like `/home/patch/Documents/python3_stuff/rig_FT232H.py` (if using Patchbox OS) or `/home/pi/Documents/python3_stuff/rig_FT232H.py` (if using Raspberry Pi OS, or just change the ‘pi’ to whatever is your main user), then it can be executed from any context. The `pisound-btn` scripts are executed from `/root/` as the current working directory (often abbreviated as CWD).

Btw, the `.` at the start of the line is a synonym for `source` command as described [here](https://linuxize.com/post/bash-source-command/) - it’s for ‘importing’ another script into the current one, so you can call some special functions defined in another script. In your case, you just want to execute your StartRig.sh script

So your StartRig.sh script should look like this:

```auto
#!/bin/sh

/usr/bin/python3 /home/patch/Documents/python3_stuff/rig_FT232H.py

# On OSes other than Patchbox OS, change 'patch' above into whatever is the main user on your system

```

Your button script should look like this:

```auto
#!/bin/sh

. /usr/local/pisound/scripts/common/common.sh

/home/patch/StartRig.sh &

flash_leds 100

```

Final thing to make sure is that `chmod +x` was done on StartRig.sh and your button script.

---

<div class="post-metadata">

**Author:** ![nburge](https://community.blokas.io/user_avatar/community.blokas.io/nburge/32/1774_2.png) [@nburge](https://community.blokas.io/u/nburge)\
**Post date:** [September 5, 2022, 4:51pm UTC](https://community.blokas.io/t/launching-a-python-program-with-the-button/3951/3 "2022-09-05T16:51:53Z")

</div>

Thank you for the explanation, I’ve edited the scripts as per your suggestion. The button is certainly running the program because the led flash operates but the python program does not…yet running the StartRig.sh script from the command line works fine…

---

<div class="post-metadata">

**Author:** ![Giedrius](https://community.blokas.io/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@Giedrius](https://community.blokas.io/u/Giedrius)\
**Post date:** [September 5, 2022, 5:12pm UTC](https://community.blokas.io/t/launching-a-python-program-with-the-button/3951/4 "2022-09-05T17:12:43Z")

</div>

You may be able to see some useful output that gets printed by your scripts in the journal:

Run:

```auto
journalctl -u pisound-btn -f

```

It will show the latest messages, so just press the button and see what output you get.

There’s some differences in the run context when scripts are executed from a systemd daemon - some required environment variables might be missing, it runs as different user (root), the current working directory might be different. You may try and add this line:

```auto
cd /home/patch

```

after the `. /usr/local/pisound/scripts/common/common.sh` line

---

<div class="post-metadata">

**Author:** ![nburge](https://community.blokas.io/user_avatar/community.blokas.io/nburge/32/1774_2.png) [@nburge](https://community.blokas.io/u/nburge)\
**Post date:** [September 5, 2022, 8:04pm UTC](https://community.blokas.io/t/launching-a-python-program-with-the-button/3951/5 "2022-09-05T20:04:56Z")

</div>

Yes…this is where it all goes wrong…see below for the journalctl output.

Sep 05 20:41:04 patchbox pisound-btn[6233]: File “/home/patch/Documents/python3\_stuff/rig\_FT232H.py”, line 8, in   
Sep 05 20:41:04 patchbox pisound-btn[6233]: import neopixel\_spi as neopixel  
Sep 05 20:41:04 patchbox pisound-btn[6233]: ModuleNotFoundError: No module named ‘neopixel\_spi’  
Sep 05 20:41:04 patchbox sudo[6234]: root : PWD=/home/patch ; USER=root ; COMMAND=/bin/sh -c echo 100 \> /sys/kernel/pisound/led  
Sep 05 20:41:04 patchbox sudo[6234]: pam\_unix(sudo:session): session opened for user root(uid=0) by (uid=0)  
Sep 05 20:41:04 patchbox sudo[6234]: pam\_unix(sudo:session): session closed for user root

---

<div class="post-metadata">

**Author:** ![nburge](https://community.blokas.io/user_avatar/community.blokas.io/nburge/32/1774_2.png) [@nburge](https://community.blokas.io/u/nburge)\
**Post date:** [September 5, 2022, 9:00pm UTC](https://community.blokas.io/t/launching-a-python-program-with-the-button/3951/6 "2022-09-05T21:00:23Z")

</div>

I got it working… I had to install a python library as root and set an environment variable in the button script then all good.

---

<div class="post-metadata">

**Author:** ![nburge](https://community.blokas.io/user_avatar/community.blokas.io/nburge/32/1774_2.png) [@nburge](https://community.blokas.io/u/nburge)\
**Post date:** [September 5, 2022, 9:28pm UTC](https://community.blokas.io/t/launching-a-python-program-with-the-button/3951/7 "2022-09-05T21:28:44Z")

</div>

Thanks so much for your help. I’d not understood the implication of the button script running as root. My python install was for user only I suppose.

---

<div class="post-metadata">

**Author:** ![nburge](https://community.blokas.io/user_avatar/community.blokas.io/nburge/32/1774_2.png) [@nburge](https://community.blokas.io/u/nburge)\
**Post date:** [September 5, 2022, 9:49pm UTC](https://community.blokas.io/t/launching-a-python-program-with-the-button/3951/8 "2022-09-05T21:49:55Z")

</div>

But now a new challenge emerges. My python program runs with one click. I then want to launch Pure Data with a double click but nothing happens… the python program seems to block any further action from the button program until I kill the Python program. I can get round this by launching PD first, but that might sometimes be inconvenient. Can you think of an alternative?

---

<div class="post-metadata">

**Author:** ![Giedrius](https://community.blokas.io/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@Giedrius](https://community.blokas.io/u/Giedrius)\
**Post date:** [September 5, 2022, 10:04pm UTC](https://community.blokas.io/t/launching-a-python-program-with-the-button/3951/9 "2022-09-05T22:04:18Z")

</div>

> [@Giedrius](#):
>
> `/home/patch/StartRig.sh`

Ah right, you should launch your script as a background job - this is done by adding a ‘&’ character at the end of a script line, so call your script as follows:

```auto
/home/patch/StartRig.sh &

```

---

<div class="post-metadata">

**Author:** ![nburge](https://community.blokas.io/user_avatar/community.blokas.io/nburge/32/1774_2.png) [@nburge](https://community.blokas.io/u/nburge)\
**Post date:** [September 6, 2022, 8:50am UTC](https://community.blokas.io/t/launching-a-python-program-with-the-button/3951/10 "2022-09-06T08:50:30Z")

</div>

YES. Amazing. 😀
