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

Reloading Python code without restarting the FreeCAD

Post by project4 » Tue Jul 17, 2018 9:26 am

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
Posts: 4361
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Reloading Python code without restarting the FreeCAD

Post by microelly2 » Tue Jul 17, 2018 10:15 am

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: 153
Joined: Fri Jul 12, 2013 12:53 pm

Re: Reloading Python code without restarting the FreeCAD

Post by project4 » Tue Jul 17, 2018 10:25 am

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

Re: Reloading Python code without restarting the FreeCAD

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

Re: Reloading Python code without restarting the FreeCAD

Post by project4 » Wed Jul 18, 2018 9:57 am

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

Re: Reloading Python code without restarting the FreeCAD

Post by Jee-Bee » Wed Jul 18, 2018 11:26 am

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: 37
Joined: Wed Oct 24, 2018 7:03 am
Location: Karlsruhe, Germany
Contact:

Re: Reloading Python code without restarting the FreeCAD

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

Re: Reloading Python code without restarting the FreeCAD

Post by christi » Sun Mar 24, 2019 9:50 am

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
Posts: 18098
Joined: Tue Mar 17, 2015 9:14 am

Re: Reloading Python code without restarting the FreeCAD

Post by chrisb » Sun Mar 24, 2019 10:07 am

I use sucessfully:

Code: Select all

import someModule
from importlib import reload
reload(someModule)
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests