36

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
2
  • Normally you'd use argparse. No idea when using ipython. Commented Mar 25, 2014 at 10:41
  • 1
    But I guess argparse also uses sys.argv to parse the arguments. Commented Mar 25, 2014 at 10:43

1 Answer 1

66

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']
wjandrea
34.1k10 gold badges69 silver badges107 bronze badges
answered Mar 25, 2014 at 10:46
Sign up to request clarification or add additional context in comments.

4 Comments

And how do you do it without specifying the file to run?
@rjurney, use ipython -i arg1 arg2
Does -- have any semantic meaning on its own, ala | and > in terminals, or is this just convention?
@TankorSmash just convention. If you look in argv you'll just see it listed as another argument. Note that git uses this extensively.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.