I have a generic class that launch an interactive session:
class SomeClass(object):
def interact(self):
from IPython import embed
embed()
and a script which use that class:
from test_ipython_class import SomeClass
global WANTED
WANTED=1234
def someFunc():
o=SomeClass()
o.interact()
someFunc()
My problem is that in the interactive session I cannot access the global variable WANTED:
PS C:\Temp\test_ipython> ls
Répertoire : C:\Temp\test_ipython
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 05/04/2020 13:36 136 test_ipython.py
-a---- 05/04/2020 13:34 102 test_ipython_class.py
PS C:\Temp\test_ipython> python .\test_ipython.py
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: WANTED
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
C:\Temp\test_ipython\test_ipython_class.py in <module>
----> 1 WANTED
NameError: name 'WANTED' is not defined
In [2]:
The question is "how to get access to globals like 'WANTED' while keeping 'SomeClass' generic, i.e. not hardcoding explicitly all globals."