How to pass a variable to magic ´run´ function in IPython

前端 未结 4 1496
-上瘾入骨i
-上瘾入骨i 2020-12-02 11:45

I want to do something like the following:

In[1]: name = \'long_name_to_type_every_now_and_then.py\'

In[2]: %run name

but this actually tr

4条回答
  •  孤街浪徒
    2020-12-02 12:25

    Use get_ipython() to get a reference to the current InteractiveShell, then call the magic() method:

    In [1]: ipy = get_ipython()
    
    In [2]: ipy.magic("run foo.py")
    ERROR: File `u'foo.py'` not found.
    

    Edit See minrk's answer — that's a much better way to do it.

提交回复
热议问题