Подтвердить что ты не робот

Как передать аргументы командной строки в ipython

Есть ли способ передать аргументы в мой python script через командную строку при использовании ipython? В идеале я хочу назвать свой script следующим:

ipython -i script.py --argument blah

и я хочу иметь --argument и blah, перечисленные в моем sys.argv.

4b9b3361

Ответ 1

До этого вы можете использовать еще один -- вариант:

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