# Compile python files?

**URL:** <https://klipper.discourse.group/t/compile-python-files/2137>\
**Category:** General Discussion\
**Created:** [February 23, 2022, 11:51am UTC](https://klipper.discourse.group/t/compile-python-files/2137 "2022-02-23T11:51:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ron](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@ron](https://klipper.discourse.group/u/ron)\
**Post date:** [February 23, 2022, 11:51am UTC](https://klipper.discourse.group/t/compile-python-files/2137/1 "2022-02-23T11:51:50Z")

</div>

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.

---

<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:** [February 23, 2022, 7:51pm UTC](https://klipper.discourse.group/t/compile-python-files/2137/2 "2022-02-23T19:51:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ron](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@ron](https://klipper.discourse.group/u/ron)\
**Post date:** [February 23, 2022, 8:59pm UTC](https://klipper.discourse.group/t/compile-python-files/2137/3 "2022-02-23T20:59:50Z")

</div>

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

---

<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:** [February 23, 2022, 9:32pm UTC](https://klipper.discourse.group/t/compile-python-files/2137/4 "2022-02-23T21:32:14Z")

</div>

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:

```auto
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)

---

<div class="post-metadata">

**Author:** ![jakep\_82](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@jakep\_82](https://klipper.discourse.group/u/jakep_82)\
**Post date:** [February 23, 2022, 10:54pm UTC](https://klipper.discourse.group/t/compile-python-files/2137/5 "2022-02-23T22:54:43Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![ron](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@ron](https://klipper.discourse.group/u/ron)\
**Post date:** [February 24, 2022, 7:44am UTC](https://klipper.discourse.group/t/compile-python-files/2137/6 "2022-02-24T07:44:54Z")

</div>

That was not obvious to me that the restart from the console was not restarting the service. Thank you to point this out.

---

<div class="post-metadata">

**Author:** ![azab2c](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/azab2c/32/5430_2.png) [@azab2c](https://klipper.discourse.group/u/azab2c)\
**Post date:** [May 29, 2023, 7:01pm UTC](https://klipper.discourse.group/t/compile-python-files/2137/7 "2023-05-29T19:01:33Z")

</div>

> [@jakep\_82](#):
>
> sudo service klipper restart

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](https://forum.v1e.com/t/mp3dp-v4-azas-build/37251/148?u=azab2c). 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!

---

<div class="post-metadata">

**Author:** ![Piezo](https://yyz2.discourse-cdn.com/free1/user_avatar/klipper.discourse.group/piezo/32/1110_2.png) [@Piezo](https://klipper.discourse.group/u/Piezo)\
**Post date:** [May 29, 2023, 11:47pm UTC](https://klipper.discourse.group/t/compile-python-files/2137/8 "2023-05-29T23:47:17Z")

</div>

> [@azab2c](#):
>
> 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?

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.
