当我使用ipython时,有没有办法通过命令行向我的Python脚本传递参数?理想情况下,我想这样调用我的脚本:
ipython -i script.py --argument blah
我希望能够在我的sys.argv
中列出--argument
和blah
。
我希望能够在我的sys.argv
中列出--argument
和blah
。
您可以在此之前使用一个或多个选项:--
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 -i arg1 arg2
。 - Omid Raha--
本身是否有语义意义,类似终端中的|
和>
,还是仅仅是惯例? - TankorSmash
argparse
也使用sys.argv
来解析参数。 - adrin