Creating a module to install and launch python

Hey there,
I’m building my new rasperryPi 4b setup around the patchboxOS and would like to start my custom python script that is my midi composer. While i found it realtively easy to install the script with a module.json I cant get it to start.

I want to run my midiTracker which works fine manually.

I then adapted the sine module to install miditracker, create a venv and install it’s pip dependencies. The source code for the module can be found here

Installing goes well, but I cant get it to launch automatically or even by activating it manually.

patchbox module activate miditracker-module

results in:

State: active_module /usr/local/patchbox-modules/imported/miditracker-module/ -> /usr/local/patchbox-modules/imported/miditracker-module/ (skip)
Environment: PATCHBOX_MODULE_ACTIVE /usr/local/patchbox-modules/imported/miditracker-module/ -> /usr/local/patchbox-modules/imported/miditracker-module/ (skip)
Manager: miditracker-module.module activated
Service: patchbox-init.service restarted

All while manually running the launch script, everything works as intended

/usr/local/patchbox-modules/imported/miditracker-module/launch.sh 

Any help is appreciated, thank you for patchboxOS.

Okay. New investigations: when actively launching the module with:

patchbox launch midiTracker-module 

I get all the echo messages from my launch.sh Script, but python3 seems not to start, or start and close. As there is no visible curses interface, nor a python3 process.

It gets started as a background process, not attached to the current console. If you need the UI to be visible on the console, you should instead create a user and start it from its .bashrc, so every time you log in with the user, the command is automatically started for that user. I think it’s best to do it as a dedicated user rather than patch or pi user.

1 Like

Thank you for the quick reply. This seems to make sense. I will try out this solution later. As I like the idea of having a module manage this interesting I’ll also try to work around the process not going to the foreground by invoking python in a new tmux session.

I’ll update this thread for anyone also interested in such a module.

Thank you again, the .bashrc file was the right way to go. Autostarting a python script that uses curses to draw the interface on boot.

  1. Create new user for this purpose alone:
sudo adduser midiuser
  1. Then add this user to the sudoers list. This is also important so the user can shut down the raspberry pi.
sudo adduser midiuser sudo
  1. reboot and do the patchbox first time usage setup

  2. optional but important in my case. Make sure that the venv exists in the right place and is setup with all the depenencies. To set up a new venv use:

python3 -m venv midiTracker/venv/
source midiTracker/venv/bin/activate
python -m pip install mido[ports-rtmidi]
  1. then edit the .bashrc
#start miditracker
source midiTracker/venv/bin/activate
python3 midiTracker/midiTracker.py

Voila. Now everytime you boot or start up a new console it activates the venv and starts up the python program.

You can still use the patchbox command to enable one of the modules or change boot behaviour.

2 Likes

Thank you!! This was the simplest way to run a Python script interactively on boot. After setting up my Python environment I simply added the last line python3 -i path/to/pythonscript.py to the end of .bashrc. It works without a screen and by ssh’ing into it. The console seems to load after jack, so my Python script requiring jack is having no problem.

Now to get a little screen (and eventually a Pisound).

1 Like