reset (isolate) Python environment

Need help, or want to share a macro? Post here!
ypsilon
Posts: 16
Joined: Tue Mar 06, 2018 9:36 pm

reset (isolate) Python environment

Postby ypsilon » Sun Nov 25, 2018 11:45 am

The Problem I have is simple. If I run a python macro all definitions persist even after the macro has finished. This means, that if I want to change something and re-run it I have to restart FreeCad alltogether. Also if I change something in a file imported from the main macro file I have to restart, so the changes are taken into account. Is there a better way to do it? I've found the following on the bug tracker (already 7 years old), but I really don't know what to do with it.

https://www.freecadweb.org/tracker/view.php?id=528

Thanks for your help in advance
triplus
Posts: 9475
Joined: Mon Dec 12, 2011 4:45 pm

Re: reset (isolate) Python environment

Postby triplus » Mon Nov 26, 2018 12:16 pm

ypsilon wrote:
Sun Nov 25, 2018 11:45 am
If I run a python macro all definitions persist even after the macro has finished.
If you run some code in Python console. Then i guess this is the expected behavior? If you run some code as a Macro (from the Macro dialog). What exact problems do you experience after?
Also if I change something in a file imported from the main macro file I have to restart, so the changes are taken into account.

Code: Select all

reload()
wmayer
Site Admin
Posts: 16471
Joined: Thu Feb 19, 2009 10:32 am

Re: reset (isolate) Python environment

Postby wmayer » Mon Nov 26, 2018 12:31 pm

Go to the preferences under General > Macro. There is an option "Run macros in local environment".
After activating it running a macro should no longer have an impact on the global environment of the interpreter.
Jee-Bee
Posts: 2131
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: reset (isolate) Python environment

Postby Jee-Bee » Mon Nov 26, 2018 1:46 pm

wmayer wrote:
Mon Nov 26, 2018 12:31 pm
Go to the preferences under General > Macro. There is an option "Run macros in local environment".
After activating it running a macro should no longer have an impact on the global environment of the interpreter.
thanks i didn't know this too... Very handy!
ypsilon
Posts: 16
Joined: Tue Mar 06, 2018 9:36 pm

Re: reset (isolate) Python environment

Postby ypsilon » Mon Nov 26, 2018 2:48 pm

thanks again!
User avatar
HarryvL
Posts: 1071
Joined: Sat Jan 06, 2018 7:38 pm
Location: Netherlands

Re: reset (isolate) Python environment

Postby HarryvL » Sun Jan 27, 2019 5:47 pm

wmayer wrote:
Mon Nov 26, 2018 12:31 pm
Go to the preferences under General > Macro. There is an option "Run macros in local environment".
After activating it running a macro should no longer have an impact on the global environment of the interpreter.
Werner, I have a similar issue: https://forum.freecadweb.org/viewtopic. ... 41#p282719. I looked at macro preferences and find that I had that option already checked. Is there a way to reset the interpreter and force it to reload modules imported by a macro?
wmayer
Site Admin
Posts: 16471
Joined: Thu Feb 19, 2009 10:32 am

Re: reset (isolate) Python environment

Postby wmayer » Sun Jan 27, 2019 6:12 pm

In Py2 there is the built-in function reload() to re-import a modified file. In Py3 the function is part of the module importlib.

No idea if there are other mechanisms to reload modules. Maybe removing the entries of sys.modules?
User avatar
HarryvL
Posts: 1071
Joined: Sat Jan 06, 2018 7:38 pm
Location: Netherlands

Re: reset (isolate) Python environment

Postby HarryvL » Sun Jan 27, 2019 7:01 pm

Thanks Werner, a good lead. I tried the following and it works:

Code: Select all

import sys
if 'myModule' in sys.modules.keys():
    del(sys.modules['myModule'])
print('myModule' in sys.modules.keys()) #FALSE
import myModule as mm
print('myModule' in sys.modules.keys()) #TRUE