如何将命令行参数传递给ipython [英] How to pass command line arguments to ipython

查看:85
本文介绍了如何将命令行参数传递给ipython的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在使用ipython时通过命令行将参数传递给我的python脚本?理想情况下,我想将我的脚本称为:

Is there any way that I pass arguments to my python script through command line while using ipython? Ideally I want to call my script as:

ipython -i script.py --argument blah

我希望能够拥有 - 参数 blah 列在我的 sys.argv 中。

and I want to be able to have --argument and blah listed in my sys.argv.

推荐答案

您可以在此之前使用一个 - 更多选项:

You can use one -- more option before that:

ipython  script.py -- --argument blah

Ipython :

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']

这篇关于如何将命令行参数传递给ipython的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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