Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command line arguments to ipython

Tags:

python

ipython

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.

like image 417
adrin Avatar asked Mar 25 '14 10:03

adrin


People also ask

How do I run IPython from command-line?

The easiest way is to run easy_install ipython[all] as an administrator (start button, type cmd , shift+right click on “cmd.exe” and select “Run as administrator”). This installs the latest stable version of IPython including the main required and optional dependencies.

How do you pass command-line arguments?

To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.

How do I pass a command-line argument in command prompt?

Every executable accepts different arguments and interprets them in different ways. For example, entering C:\abc.exe /W /F on a command line would run a program called abc.exe and pass two command line arguments to it: /W and /F. The abc.exe program would see those arguments and handle them internally.


1 Answers

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'] 
like image 129
Omid Raha Avatar answered Oct 13 '22 05:10

Omid Raha