在脚本中从IPython交互命名空间访问变量 [英] Accessing variables from IPython interactive namespace in a script

查看:150
本文介绍了在脚本中从IPython交互命名空间访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来访问IPython交互式命名空间中的变量。在实现具有慢加载命令的项目时,我想运行一个脚本将数据加载到交互式工作区,然后调用第二个使用数据的脚本,就像使用MATLAB一样。

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 the data into the interactive work space, then call a second script that uses the data, like is possible with MATLAB.

在这个简单的例子中,我想做的是

In this simple case, what I want to do is

In [20]: a=5

In [21]: run tst

其中tst.py只是

print a

我的想法是,我想运行一次加载脚本,然后只需要在tst.py上运行。

The idea is that I want to run the loading script once, then just work on tst.py.

谢谢!

推荐答案

尝试在IPython的magic run命令中使用-i选项;它使脚本使用当前的交互命名空间运行,例如与

Try using the -i option on IPython's magic run command; it makes the script run using the current interactive namespace, e.g. with

load.py:

a = 5

tst.py:

print a

从IPython我得到;

From IPython I get;

In [1]: from load import *

In [2]: run -i tst
5

这篇关于在脚本中从IPython交互命名空间访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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