How to pass a variable as a command line argument in iPython?

拥有回忆 提交于 2019-12-10 17:46:52

问题


For example, I have a Python module and a iPython module,

cmd.py

import sys
print sys.argv

test.ipynb

for i in range(5):
   %run cmd.py 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?


回答1:


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

from http://ipython.readthedocs.io/en/stable/interactive/reference.html

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



来源:https://stackoverflow.com/questions/41507829/how-to-pass-a-variable-as-a-command-line-argument-in-ipython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!