Can't Get GCode System Command Plugin to work - Please Help?

I have been trying for two days now to get this to work and I just can't get there - please help!

I have a servo set up and run through an Adafruit 16 channel PWM servo hat on my Octopi. Its purpose is to use a servo to deploy a brass bristle brush over the print bed that the extruder then passes over before a print to clean the nozzle of any hanging "buggers" before starting a print.

The board is wired and functioning properly, all libraries are installed. I created two python scripts, one to deploy the brush called deploy.py:
import time
from adafruit_servokit import ServoKit
kit = ServoKit(channels=16)
kit.servo[0].angle = 110

and one to retract the brush after cleaning called retract.py:
import time
from adafruit_servokit import ServoKit
kit = ServoKit(channels=16)
kit.servo[0].angle = 0

logged in as pi in a terminal I can run those scripts successfully by issuing "python3 deploy.py" or "python3 retract.py" from the home directory (/home/pi). I searched FOREVER to find a way to pass system commands from GCode in my print files to the OS layer to activate that servo, and FINALLY I found and installed the GCode System Commands plugin. I thought Hurray! An Answer! Got the plugin installed and at first I tried setting OCTO10 to the command line python3 /home/pi/deploy.py. Issue OCTO10 from teh Octoprint Terminal - error - nothing happens with the brush. So then as I looked, I thought OK, looks like all the examples are shell commands so I created shell scripts for each (you guessed it - Deploy.sh and retract.sh):

deploy.sh:
#!/bin/bash
python3 /home/pi/deploy.py

retract.sh:
#!/bin/bash
python3 /home/pi/deploy.py

I have tried EVERY single permutation of that shell script I can think of, including trying the ultra-stupid of issuing sudo for it and passing the password hard-coded in the script, JUST to see if I could get the damn thing to work (and yes, both shell scripts are set to executable). No go - always results in:

Traceback (most recent call last):
File "/home/pi/deploy.py", line 2, in
from adafruit_servokit import ServoKit
ImportError: No module named 'adafruit_servokit'

I can NOT figure out for the LIFE of me WHY it knows where the module is, and properly executes it from the Raspberry terminal command line, yet issue exactly the same command in a shell script and it blows up saying it can't find a module that is indeed there. Of course with this, any attempt to run either the shell script, or just the plain python command line in the plugin ALWAYS results in:

Exec(GCodeSystemCommands): OCTO10
Return(GCodeSystemCommands): error

Which, is so helpfully verbose..... Please, can anyone set an old guy straight?

Try changing this to a python bash as the first line in your py file, and make sure you are using full paths to the py file that you are running.

#!/usr/bin/python3

By the way, I have seen the Easy Servo plugin used to do exactly what you are describing.

I have tried that as well, with an equal amount of success. Well, less actually because I could never get it to move the servo, hence the continuation of the hunt. Thanks! I'll try changing the bash. I'll let you know

No luck - same result changing to #!/usr/bin/python3 - same result

Just to confirm, you added that line to the top of this file?

I should add some more logging options at some point. For now see if there is any difference in your path variable. In your py script add:

import sys
print('path=', sys.path)

Run it using whatever method was working and record the value then try using OCTO10(or whichever) having your bash wrapper log the result to a file. Lets see what we get.

Yes, definitely

"Yes definitely" was to the one above you.

First, thank you so much for helping me. You're saving me a lot of hair because I was truly getting to the point of pulling it out, and I ain't got that much anymore!

Here is is the path result from python:
path= ['/home/pi', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/home/pi/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/site-packages']

Here is the script I modified/ran for the bash capture. The shell script is what OCTO10 calls:
#!/bin/bash
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>log.out 2>&1

Everything below will go to the file 'log.out':

python3 /home/pi/deploy.py

and here is the capture result:
path= ['/home/pi', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-arm-linux-gnueabihf', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/local/lib/python3.5/dist-packages/myDevices-2.0.3-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/requests-2.22.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/psutil-5.6.3-py3.5-linux-armv7l.egg', '/usr/local/lib/python3.5/dist-packages/paho_mqtt-1.4.0-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/netifaces-0.10.9-py3.5-linux-armv7l.egg', '/usr/local/lib/python3.5/dist-packages/jsonpickle-1.2-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/enum34-1.1.6-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/urllib3-1.25.3-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/idna-2.8-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/chardet-3.0.4-py3.5.egg', '/usr/local/lib/python3.5/dist-packages/certifi-2019.6.16-py3.5.egg', '/usr/lib/python3/dist-packages']
Traceback (most recent call last):
File "/home/pi/deploy.py", line 4, in
from adafruit_servokit import ServoKit
ImportError: No module named 'adafruit_servokit'

MAYBE I see it? the path from bash refers to python3.5, but I have 3.7.4 I think it is installed, and I used pip3 to install the Adafruit packages, and I BELIEVE, at least I sure thought I had python3 aliased to python 3.7, not 3.5. Forgive me, I am just about at my limit around working in a Linux prompt.

So take a look at what bins/symlinks are made with ls -lad /usr/bin/python3*. You'll likely want to call the script with /usr/bin/python3.7 or change the shebang inside the script with #!/usr/bin/python3.8.

Hallelujah!!! THANK YOU THANK YOU THANK YOU! Simply changing the command call in the shell script to absolute (python3.7 deploy.py) worked! THANK YOU! Now, tells me I need to clean up my python installations somehow, though I can not completely remove python2 as I understand there is functionality that depends on it, which is kinda funny to me since Octoprint complains I am running python2, and now, will not let me do any installs/upgrades/updates through the interface. But hey - my original problem is working now, so I'm happy!

This is more than likely related to the pip version in OctoPrint's venv or a logging module that was loaded by PTG a while back that broke something. You should check out this...

OK - I had to install a new Octopi image, because the last one went toast on me. This one came with python 3.7.4 already, and I've got the python scripts rebuilt, and the shell command that runs them working fine, but trying to call them from inside Octoprint to deploy/retract that brush returns : "Recv: echo:Unknown command: "OCTO15.sh". Help again? Running the latest version of Octoprint/pi.

BTW - I am trying to call them from both the command line in Octoprint, AND from a script built into my print files.

THOUGHT I had it. Realized this was executing through the plugin GCode System Commands which was not installed. Installed the plug in, built OCTO15 (deploy) and OCTO25 (retract) in the plug in. Execute it from the terminal in Octoprint, shows the command passing successfully and then returns "error".

reran the path insertion into the python script from the last time and ran from the terminal inside an ssh terminal. Results:
path= ['/home/pi', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/pi/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']
pi@octopi:~ $

did you have to rebuild the script from scratch? if so, did you chmod it to make it executable?

sudo chmod +x /path/to/script/OCTO15.sh

Yes, I did

I can run it flawlessly from the command line (/home/pi/OCTO15.sh). Just can't get it to run from GCode Command plugin