Page 1 of 2

Reloading Python code without restarting the FreeCAD

Posted: Tue Jul 17, 2018 9:26 am
by project4
Hi guys,

I'm trying to help with a WorkBench development written in Python.
Is there a way to reload python code that belongs to the WB without the need to restart the whole FreeCAD application?
Switching to another WB and back didn't help :(

Thanks.

Re: Reloading Python code without restarting the FreeCAD

Posted: Tue Jul 17, 2018 10:15 am
by microelly2
force a reload in the Activatewd method of your command

Code: Select all


		Gui.doCommand("import " + modul)
		Gui.doCommand("import " + self.lmod)
		Gui.doCommand("reload(" + self.lmod + ")")
                Gui.doCommand(self.command)


Re: Reloading Python code without restarting the FreeCAD

Posted: Tue Jul 17, 2018 10:25 am
by project4
Thanks, will try.

Re: Reloading Python code without restarting the FreeCAD

Posted: Wed Jul 18, 2018 9:43 am
by triplus
Usually doing:

Code: Select all

reload(SomeModule.py)
Works just fine. That is when you changed code in SomeModule.py and it was already imported.

Re: Reloading Python code without restarting the FreeCAD

Posted: Wed Jul 18, 2018 9:57 am
by project4
Thanks!

Re: Reloading Python code without restarting the FreeCAD

Posted: Wed Jul 18, 2018 11:26 am
by Jee-Bee
triplus wrote:
Wed Jul 18, 2018 9:43 am
Usually doing:

Code: Select all

reload(SomeModule.py)
Works just fine. That is when you changed code in SomeModule.py and it was already imported.
i remember that is don't work out of the box with py3...

Re: Reloading Python code without restarting the FreeCAD

Posted: Sun Mar 24, 2019 9:37 am
by christi
Jee-Bee wrote:
Wed Jul 18, 2018 11:26 am
i remember that is don't work out of the box with py3...
Any ideas how reloading modules works with python 3 ?

Re: Reloading Python code without restarting the FreeCAD

Posted: Sun Mar 24, 2019 9:50 am
by christi
christi wrote:
Sun Mar 24, 2019 9:37 am
Jee-Bee wrote:
Wed Jul 18, 2018 11:26 am
i remember that is don't work out of the box with py3...
Any ideas how reloading modules works with python 3 ?
This works with Python 3:

Code: Select all

reload(MyModule)
but it does not work, if you have added your module to the GUI:

Code: Select all

FreeCADGui.addCommand('MyModule', MyModule())
The solution can be creating a sub class that is called in the MyModule class

Re: Reloading Python code without restarting the FreeCAD

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

Code: Select all

import someModule
from importlib import reload
reload(someModule)

Re: Reloading Python code without restarting the FreeCAD

Posted: Thu Feb 06, 2020 4:18 pm
by steve123
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).