如何在iPython中将变量作为命令行参数传递? [英] How to pass a variable as a command line argument in iPython?
问题描述
例如,我有一个Python模块和一个iPython模块,
For example, I have a Python module and a iPython module,
cmd.py
import sys
print sys.argv
test.ipynb
test.ipynb
for i in range(5):
%run cmd.py i
当我运行test.ipynb时,它只是多次打印['cmd.py','i']。我怎样才能传递i的值而不是'i'作为命令行参数?
When I run test.ipynb, it just print ['cmd.py', 'i'] multiple times. How can I pass the value of i instead of 'i' as a command line argument?
推荐答案
shell模式从字面上解释所有内容(否则 cmd.py
也会被评估)。我会这么做:
the shell mode interprets everything literally (else cmd.py
would be evaluated as well). I would just do:
for i in range(5):
%run cmd.py $i
来自 http://ipython.readthedocs.io/en/stable/interactive/reference.html
IPython还允许您在进行系统调用时扩展python变量的值。在{braces}中包装变量或表达式:
IPython also allows you to expand the value of python variables when making system calls. Wrap variables or expressions in {braces}:
对于简单的情况,您也可以将$添加到变量名称
For simple cases, you can alternatively prepend $ to a variable name
这篇关于如何在iPython中将变量作为命令行参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!