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 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.
What are you trying to achieve?
If you are just tinkering with the python files, a simple restart of the Klipper service should be enough for them to take effect
I already tried a restart of Klipper (through mainsail) without success. Then I went to find other solutions.
If restart is the proper way, then I need to see why it didn’t work.
How Klipper know there is a new file to compile? The file I edited is ~/klipper/klippy/extras/hall_filament_width_sensor.py
I’m by no means an expert but my understanding is:
When a Python program is started, it checks if the “source” .py files are untouched. If yes, it uses the created byte-code files (.pyc) and simply starts. If no, it “compiles” the changed .py file to a new .pyc file.
My Klipper install at least does this:
ubuntu@ubuntu ~/klipper/klippy/extras $ stat virtual_sdcard.pyc
File: virtual_sdcard.pyc
Size: 12267 Blocks: 24 IO Block: 4096 regular file
Device: b302h/45826d Inode: 375677 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ ubuntu) Gid: ( 1000/ ubuntu)
Access: 2022-02-22 15:55:32.201400765 +0000
Modify: 2021-11-06 08:50:25.598437235 +0000
Change: 2021-11-06 08:50:25.598437235 +0000
Birth: -
ubuntu@ubuntu ~/klipper/klippy/extras $ nano virtual_sdcard.py
ubuntu@ubuntu ~/klipper/klippy/extras $ stat virtual_sdcard.pyc
File: virtual_sdcard.pyc
Size: 12267 Blocks: 24 IO Block: 4096 regular file
Device: b302h/45826d Inode: 375677 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ ubuntu) Gid: ( 1000/ ubuntu)
Access: 2022-02-22 15:55:32.201400765 +0000
Modify: 2021-11-06 08:50:25.598437235 +0000
Change: 2021-11-06 08:50:25.598437235 +0000
Birth: -
ubuntu@ubuntu ~/klipper/klippy/extras $ sudo service klipper restart
ubuntu@ubuntu ~/klipper/klippy/extras $ stat virtual_sdcard.pyc
File: virtual_sdcard.pyc
Size: 12268 Blocks: 24 IO Block: 4096 regular file
Device: b302h/45826d Inode: 375677 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ ubuntu) Gid: ( 1000/ ubuntu)
Access: 2022-02-23 21:25:23.232070725 +0000
Modify: 2022-02-23 21:25:23.232070725 +0000
Change: 2022-02-23 21:25:23.232070725 +0000
Birth: -
See the “Change” timestamp: After the edit nothing happens, and after the service restart the pyc is recreated.
(Now I hope that a real developer does not kill me for this noobish explanation)
When you say you restarted Klipper thrugh Mainsail, what exactly do you mean? If you typed restart into the console, all you’ve done is essentially reload the printer.cfg. You need to restart the Klipper service for file changes to take effect.
sudo service klipper restart
That was not obvious to me that the restart from the console was not restarting the service. Thank you to point this out.
Is this the fastest way to force reloading of edited /extras python files, like z_tilt.py? Asking incase there’s some dynamic runtime reloading command am not aware of to help speed up Dev inner loop?
My goal… Currently trying to better understand why z_tilt isn’t behaving as expected for me. So, am adding some tracing, and will start separate topic after digging deeper. I want to first try self helping/investigating to figure out what I’m doing wrong. Cheers!
Yes this is the fastest way.
You could use Python’s importlib.reload
to reload the module, but the previously instantiated printer object will not be updated. There is no way to unregistered a printer object during runtime. In the end, you’d have to trigger a RESTART
/printer.request_exit('restart')
in order to reload all the printer objects. There is likely very little gains over a systemctl restart klipper
.
One thing to note is that having many macros in the config can slows the initialization of klippy.