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

adr*_*rin 27 python ipython

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

ipython -i script.py --argument blah
Run Code Online (Sandbox Code Playgroud)

我希望能够拥有--argumentblah列入我的sys.argv.

Omi*_*aha 49

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

ipython  script.py -- --argument blah
Run Code Online (Sandbox Code Playgroud)

帮助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.
Run Code Online (Sandbox Code Playgroud)

演示:

$ 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']
Run Code Online (Sandbox Code Playgroud)

  • 如何在不指定要运行的文件的情况下执行此操作? (2认同)