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!
project4
Posts: 237
Joined: Fri Jul 12, 2013 12:53 pm

Reloading Python code without restarting the FreeCAD

Post 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.
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 »

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)

project4
Posts: 237
Joined: Fri Jul 12, 2013 12:53 pm

Re: Reloading Python code without restarting the FreeCAD

Post by project4 »

Thanks, will try.
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: Reloading Python code without restarting the FreeCAD

Post 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.
project4
Posts: 237
Joined: Fri Jul 12, 2013 12:53 pm

Re: Reloading Python code without restarting the FreeCAD

Post by project4 »

Thanks!
Jee-Bee
Veteran
Posts: 2566
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: Reloading Python code without restarting the FreeCAD

Post 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...
User avatar
christi
Posts: 203
Joined: Wed Oct 24, 2018 7:03 am
Location: Karlsruhe, Germany
Contact:

Re: Reloading Python code without restarting the FreeCAD

Post 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 ?
User avatar
christi
Posts: 203
Joined: Wed Oct 24, 2018 7:03 am
Location: Karlsruhe, Germany
Contact:

Re: Reloading Python code without restarting the FreeCAD

Post 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
chrisb
Veteran
Posts: 53922
Joined: Tue Mar 17, 2015 9:14 am

Re: Reloading Python code without restarting the FreeCAD

Post by chrisb »

I use sucessfully:

Code: Select all

import someModule
from importlib import reload
reload(someModule)
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
steve123
Posts: 149
Joined: Fri Sep 04, 2015 8:58 pm

Re: Reloading Python code without restarting the FreeCAD

Post 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).
Post Reply