In the IPython (Spyder) console, is it possible to access the variable from main () after starting my program?

I am running my code using the IPython Console. I have a main() function that runs as follows:

 if __name__ == "__main__": main() 

Then main() calls a bunch of other functions, and I don't use any classes. My question is: can I print or manipulate my variables interactively in the console after starting my program?

As if I don't have main() or any other functions, for example, I can declare in my code:

 a=1 

And then itโ€™s easy to access from the console:

 In [20]: a Out[20]: 1 

Thanks in advance if you can help with this noob question.

+5
source share
1 answer

Have main return locals() .

 def main(): # ... return locals() if __name__ == "__main__": locals().update(main()) 

See also Adding function variables from an imported module to the iPython interactive namespace and Dump function variables to a workspace in python / ipython

+1
source

Source: https://habr.com/ru/post/1208186/


All Articles