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
and I want to be able to have --argument and blah listed in my sys.argv.
asked Mar 25, 2014 at 10:33
adrin
4,9865 gold badges38 silver badges54 bronze badges
1 Answer 1
You can use one -- more option before that:
ipython script.py -- --argument blah
Help of 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']
Sign up to request clarification or add additional context in comments.
4 Comments
rjurney
And how do you do it without specifying the file to run?
Omid Raha
@rjurney, use
ipython -i arg1 arg2TankorSmash
Does
-- have any semantic meaning on its own, ala | and > in terminals, or is this just convention?Nathan Chappell
@TankorSmash just convention. If you look in argv you'll just see it listed as another argument. Note that git uses this extensively.
lang-py
argparsealso usessys.argvto parse the arguments.