Reloading Python code without restarting the FreeCAD
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Re: Reloading Python code without restarting the FreeCAD
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
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
- microelly2
- Veteran
- Posts: 4688
- Joined: Tue Nov 12, 2013 4:06 pm
- Contact:
Re: Reloading Python code without restarting the FreeCAD
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
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).chrisb wrote: ↑Sun Mar 24, 2019 10:07 am I use sucessfully:Code: Select all
import someModule from importlib import reload reload(someModule)
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
If you want to reply, open a new topic, you are necrobumping a 3yo topic.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).
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/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/