如何将命令行参数传递给IPython?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (153)

在使用IPython时,我是否可以通过命令行将参数传递给python脚本?理想情况下,我想将我的脚本称为:

ipython -i script.py --argument blah

我希望能将--argumentblah列在我的sys.argv里。

提问于
用户回答回答于

你可以用一个--在此之前还有更多选择:

ipython  script.py -- --argument blah
ipython [subcommand] [options] [-c cmd | -m mod | file] [--] [arg] ...

If invoked with no options, it executes the file and exits, passing the
remaining arguments to the script, just as if you had specified the same
command with python. You may need to specify `--` before args to be passed
to the script, to prevent IPython from attempting to parse them. If you
specify the option `-i` before the filename, it will enter an interactive
IPython session after running the script, rather than exiting.

演示:

$ cat script.py 
import sys
print(sys.argv)

$ ipython  script.py -- --argument blah
['script.py', '--argument', 'blah']

$ ipython  script.py -- arg1 arg2
['script.py', 'arg1', 'arg2']

扫码关注云+社区

领取腾讯云代金券