在IPython中使用Interactive Shell外部的魔术命令 [英] Using magic commands outside of Interactive Shell in IPython

查看:246
本文介绍了在IPython中使用Interactive Shell外部的魔术命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从外部文件中使用来自IPython的魔术命令?例如,如果我有一个文件,rcode.py代码为:

Is there a way to use "magic commands" from IPython from an outside file? For example if I have a file, "rcode.py" with the code:

%load_ext rmagic
%R a=c(1,2,3);b=c(2,3,4);print(summary(lm(a~b)))

当我使用 ipython rcode.py运行第一行时,这给了我一个 SyntaxError 在命令行中。然而,当我用ipython直接在交互式shell中键入这些行时,它运行正常。这是因为你只在交互式shell中做魔术吗?

This gives me a SyntaxError for the first line when I run it using ipython rcode.py in the command line. However when I type these lines straight into the interactive shell with ipython it runs fine. Is this because you only do magic in the interactive shell?

谢谢!

推荐答案

如果使用 .ipy 扩展名命名文件,ipython将正确解析它。如果需要,你可以简单地创建一个符号链接:

If you name your file with a .ipy extension, ipython will parse it properly. You can simply make a symlink if you want:

$ ln -s rcode.py rcode.ipy
$ ipython rcode.ipy

这篇关于在IPython中使用Interactive Shell外部的魔术命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆