Accessing variables from IPython interactive namespace in a script

前端 未结 2 763
借酒劲吻你
借酒劲吻你 2021-02-19 00:08

Is there an easy way to access variables in the IPython interactive namespace. While implementing a project that has a slow load command, I would like to run a script to load t

2条回答
  •  醉话见心
    2021-02-19 01:03

    There is no easy or smart way to do this. One way would be to have a main function in your test function and then pass in the globals from your environment to update the globals in the caller. For example:

    tst.py

    def main(borrowed_globals):
        globals().update(borrowed_globals)
        print a
    

    And then in iPython:

    In [1]: a = 5
    
    In [2]: import tst
    
    In [3]: tst.main(globals())
    5
    

提交回复
热议问题