Running a python script once a channel is triggered

The executeCommand function is not running in an environment that has access to the full path data of the regular environment. It’s not the /home/rook/command.py that can’t be found but the python alias. You will have to give the command the full path to the python executable.

Your python executable is probably located at /usr/bin/python but you can check from the terminal:

$ type python
python is /usr/bin/python

OH3 uses a differnt syntax for executeCommandLine than OH2 does.
As long as you do not need to wait for the execution run

executeCommandLine("sudo","python","/home/rook/command.py")

In case it does not work use the full path for the executables.

So once I give it the full path to the python executable, how does it determine what py script I want it to run? I made the command.py by writing it to a text file and saving it as .py, it still works when executed in the command line manually.

In this case the command python is just a shortcut that is automatically made when you install python. The actual file that runs python has a location just like everything else. The issue is that in linux, not every user has access to the same set of these shortcuts for commands. Your instance of OH is running under a certain user, which depending on your current setup may or may not be the same user as what you see when you open a terminal. If OH is running under a different user: 1) you may in fact have different behaviors between commands that you test in a terminal and commands that are run from OH and 2) instead of just using python to run your script you may have to use the full path. Nothing else about the command changes, just the form of the python reference:
sudo /usr/bin/python /home/rook/command.py

As Wolfgang_S says, it may be that your only problem is the formatting of the executeCommand which changed in OH3. But, if you try that correct formatting and it still doesn’t work, then you likely have to use the full path for the python executable:
executeCommandLine("sudo","/usr/bin/python","/home/rook/command.py")

1 Like