# Add python code/script to klipper

**URL:** <https://klipper.discourse.group/t/add-python-code-script-to-klipper/9061>\
**Category:** General Discussion\
**Created:** [June 20, 2023, 9:32am UTC](https://klipper.discourse.group/t/add-python-code-script-to-klipper/9061 "2023-06-20T09:32:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![masterq](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@masterq](https://klipper.discourse.group/u/masterq)\
**Post date:** [June 20, 2023, 9:32am UTC](https://klipper.discourse.group/t/add-python-code-script-to-klipper/9061/1 "2023-06-20T09:32:33Z")

</div>

Hello \*,  
I think I just made a mistake, but as I analyzed it now for some hours and decided to ask now.

I want to add a script to my python code. What I did:

```auto
#loged in via ssh as pi in a MainsailOS default debian image
service klipper stop
cd ~/klipper/klippy/extras/
wget https://github.com/th33xitus/kiauh/raw/master/resources/gcode_shell_command.py
service klipper start

```

I did this on 2 printers, with a very similar software setup. On one the file is recognized, and the command is available on the other the printer the python code is not compiled, and the command is not available.

I did restart klipper and the printer, renamed the file, retouched the file, compiled it by hand, checked the permission… But no matter what I do, there will not be generated a pyc file (except I generate it myself). So my conclusion is that I miss something.

I did read this, but could not find more information:

> [@Compile python files?](https://klipper.discourse.group/t/compile-python-files/2137):
>
> I modified a python file in /klippy/extras. What do I do to compile it on a raspberrypios? I followed here [Packaging Klipper - Klipper documentation](https://www.klipper3d.org/Packaging.html) that I can run python2 -m compileall klippy. But it does not produce the same file as when I compile firmware through kiauh (which force a python compilation too). Then I have no clue what is the proper method to do it.

I could not find any hint on the logs.

On this printer it works flawless:  
log deleted

On this printer the python file will not be recognized:  
log deleted

Same python version same patch level on os. Same klipper patch level was tested.

Thanks for reading 🙂

---

<div class="post-metadata">

**Author:** ![Sineos](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/sineos/32/18_2.png) [@Sineos](https://klipper.discourse.group/u/Sineos)\
**Post date:** [June 20, 2023, 9:43am UTC](https://klipper.discourse.group/t/add-python-code-script-to-klipper/9061/2 "2023-06-20T09:43:42Z")

</div>

From a quick look it seems you did not define a shell command but only try to call one.  
Ref to [kiauh/docs/gcode\_shell\_command.md at master · th33xitus/kiauh · GitHub](https://github.com/th33xitus/kiauh/blob/master/docs/gcode_shell_command.md)

---

<div class="post-metadata">

**Author:** ![masterq](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@masterq](https://klipper.discourse.group/u/masterq)\
**Post date:** [June 20, 2023, 9:45am UTC](https://klipper.discourse.group/t/add-python-code-script-to-klipper/9061/3 "2023-06-20T09:45:21Z")

</div>

I did forget that I removed the inclusion of the file where I included the shell command!

Thanks!!! You are so helpful and so fast!

So the conclusion is:  
The file was not respected because it was not used correct?  
Or more precisely, the file was never compiled because the code did never run?

---

<div class="post-metadata">

**Author:** ![Sineos](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/sineos/32/18_2.png) [@Sineos](https://klipper.discourse.group/u/Sineos)\
**Post date:** [June 20, 2023, 9:53am UTC](https://klipper.discourse.group/t/add-python-code-script-to-klipper/9061/4 "2023-06-20T09:53:58Z")

</div>

```auto
Starting Klippy...
Args: ['/home/pi/klipper/klippy/klippy.py', '/home/pi/klipper_config/printer.cfg', '-l', '/home/pi/klipper_logs/klippy.log', '-a', '/tmp/klippy_uds']
Git version: 'v0.11.0-83-g5addedd3-dirty'
Untracked files: klippy/extras/gcode_shell_command.py

```

- From the log it seems that the file is well respected
- The error `Unknown command:"RUN_SHELL_COMMAND"` might be misleading in the sense that Klipper knows `RUN_SHELL_COMMAND` but does not know `CMD=signal_light`, since you forgot to define it via `[gcode_shell_command signal_light]`
