Page 2 of 2

Re: Reloading Python code without restarting the FreeCAD

Posted: Thu Feb 06, 2020 4:45 pm
by Kunda1

Re: Reloading Python code without restarting the FreeCAD

Posted: Fri Feb 07, 2020 7:52 am
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.

Re: Reloading Python code without restarting the FreeCAD

Posted: Thu Oct 05, 2023 9:13 pm
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()

Re: Reloading Python code without restarting the FreeCAD

Posted: Fri Oct 06, 2023 6:42 am
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.