1

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."

acapola
  • 1,078
  • 1
  • 12
  • 23
  • You are trying to access a global variable of different file? If so, try reading this: https://stackoverflow.com/questions/3400525/global-variable-from-a-different-file-python – Amiram Apr 05 '20 at 13:28
  • Thanks for the pointer but my aim is different: I am trying to have a generic class "SomeClass" which encapsulate IPython.embed() AND which offer to the user access to globals from his file (as well as locals of the scope which invoked "SomeClass.interact()". – acapola Apr 07 '20 at 22:02

0 Answers0