Reloading Python code without restarting the FreeCAD

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Reloading Python code without restarting the FreeCAD

Post by Kunda1 »

Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Reloading Python code without restarting the FreeCAD

Post by microelly2 »

steve123 wrote: Thu Feb 06, 2020 4:18 pm It would be handy for debugging if there was a button somewhere (or maybe just a "developer" menu item) that would allow you to force FreeCAD to reload a module(s).
You can hack your own reolad where you want.
But there cannot be a general solution.
If you have created python objects and you reload the class definitons you will get unexpected results or in worst case crashes.
So you should know what you do when you force a reload.
aavogt
Posts: 12
Joined: Thu Oct 05, 2023 8:52 pm
Contact:

Re: Reloading Python code without restarting the FreeCAD

Post by aavogt »

chrisb wrote: Sun Mar 24, 2019 10:07 am I use sucessfully:

Code: Select all

import someModule
from importlib import reload
reload(someModule)
Based on the above, I put femloader.py and fem.py in ~/.local/share/FreeCAD/Macro/. After I save fem.py it gets reloaded automatically. I was hoping freecad would already do such a thing already since it will ask "This has been modified outside of the source editor. Do you want to reload it?" for .FCMacro files. So it can likely be done more portably (without pyinotify).

Code: Select all

# femloader.py
import sys
import os.path
sys.path += ["/usr/lib/python3/dist-packages/"] # why is this here for accessing ubuntu's python3-pyinotify?
import pyinotify
import fem
from importlib import reload

def reload_fem(event):
    reload(fem)
    print(f"reloaded fem from {event.pathname}")

def reloader():
    wm = pyinotify.WatchManager()
    femfile = os.path.join(os.path.dirname(__file__) , "fem.py")
    wm.add_watch(femfile, pyinotify.IN_MODIFY)
    notifier = pyinotify.ThreadedNotifier(wm, reload_fem)
    notifier.start()
    return notifier

notifier = reloader()
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Reloading Python code without restarting the FreeCAD

Post by onekk »

aavogt wrote: Thu Oct 05, 2023 9:13 pm ...
Based on the above, I put femloader.py and fem.py in ~/.local/share/FreeCAD/Macro/. After I save fem.py it gets reloaded automatically. I was hoping freecad would already do such a thing already since it will ask "This has been modified outside of the source editor. Do you want to reload it?" for .FCMacro files. So it can likely be done more portably (without pyinotify).
If you want to reply, open a new topic, you are necrobumping a 3yo topic.

Better not.

IMHO a macro is standalone file, not "multifile macros" as this is "out of scope" of a macro.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply