如何将命令行参数传递给 ipython

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22631845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:18:52  来源:igfitidea点击:

How to pass command line arguments to ipython

pythonipython

提问by adrin

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 时通过命令行将参数传递给我的 python 脚本?理想情况下,我想将我的脚本称为:

ipython -i script.py --argument blah

and I want to be able to have --argumentand blahlisted in my sys.argv.

我希望能有--argumentblah我列出的sys.argv

采纳答案by Omid Raha

You can use one --more option before that:

--在此之前,您可以再使用一个选项:

ipython  script.py -- --argument blah

Help of Ipython:

帮助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.

Demo:

演示:

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