3

How can we start ipython REPL and instruct it to pass some command-line arguments to the underlying python interpreter?

For example, we can open a python REPL with increased verbosity by using

python -v

But I could not see how to pass that flag through when opening IPython.

wim
  • 338,267
  • 99
  • 616
  • 750
  • 2
    In a pinch, you could use the [environment variables](https://docs.python.org/3/using/cmdline.html#environment-variables) instead, e.g. `PYTHONVERBOSE`. – Thomas K Mar 07 '16 at 23:01
  • The comment by @ThomasK solved this problem! – taper Aug 30 '18 at 21:46

3 Answers3

5

I'd say the best way to do that would be to explicitly launch ipython with python:

python /usr/bin/ipython

as the ipython executable is just a python script ; or you can launch ipython by telling python to load the ipython library:

python -m IPython.frontend.terminal.ipapp

and then you can add all the native python arguments:

python -v /usr/bin/ipython
python -v -m IPython.frontend.terminal.ipapp

HTH

zmo
  • 24,463
  • 4
  • 54
  • 90
  • 1
    there's no pretty way to do this, and truth is, I'm not using `ipython`, because I consider that a well configured (with completion and help) vanilla python REPL is perfectly fine, and is exactly what's gonna run the code I'm using it with, so no surprises, no differences between the script and the REPL. From my perspective, ipython is just a hack that cannot get quite ugly at times ☺ – zmo Mar 07 '16 at 22:22
  • Looks to me like this is simply a shebang script issue. The interpreter does not use the `sys.argv` when the script is called directly. – hpaulj Mar 09 '16 at 17:50
  • 1
    Incidentally it is, and you can add interpreter parameters within the shebang line. – zmo Mar 09 '16 at 17:59
1

You could write your own ipython shebang script.

Here I copied my ipython script and added the -v

#!/usr/local/bin/python3.5 -v

# -*- coding: utf-8 -*-
import re
import sys

from IPython import start_ipython

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(start_ipython())

Now when I execute ./vipython I get many pages of import information upon startup and shutdown.

I gather from other SO questions that I might not be able to add multiple options to such a shebang line.

How to use multiple arguments with a shebang (i.e. #!)?

So for example

#!/usr/local/bin/python3.5 -vv

works, but

#!/usr/local/bin/python3.5 -v -v

doesn't - it gives me

1008:~/mypy$ ./vipython
Unknown option: - 
usage: /usr/local/bin/python3.5 [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.
Community
  • 1
  • 1
hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

That -v option affects the behavior of the interpreter itself, not just the REPL. You get the extra import information regardless of whether you add the -i option.

Here's the default script that launches ipython (or at least one version of that)

1522:~/mypy$ cat /usr/local/bin/ipython3.5
#!/usr/local/bin/python3.5

# -*- coding: utf-8 -*-
import re
import sys

from IPython import start_ipython

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(start_ipython())

and from within an ipython session:

In [1153]: from IPython import start_ipython
In [1154]: start_ipython??
String form: <function start_ipython at 0xb697edac>
File:        /usr/lib/python3/dist-packages/IPython/__init__.py
Definition:  start_ipython(argv=None, **kwargs)
Source:
def start_ipython(argv=None, **kwargs):
   "..."
   from IPython.terminal.ipapp import launch_new_instance
    return launch_new_instance(argv=argv, **kwargs)

Eventually the argv are passed to an argparse parser. Ipython populates that parser with arguments derived from its config files. So you have several options for setting parameters - default config, profile config, and commandline. But all of this is after the interpreter has been launched. Some things are acted on in the same was a with an interpreter REPL, but not all (-m, but not -v).

When -v is used as zmo suggests, we see all the imports of ipython code - which are quite a few. Are you interested in those, or are you more interested in imports related to your own script?

I use ipython a lot to test answers, especially for numpy. In fact my default ipython call has the --pylab flag. But to test stand alone scripts I use plain python (often called from a terminal window in my editor). Sometimes though I'll %run a script from within ipython. That loads the module into the main workspace, making it easy to perform %timeit tests on functions.


Other ipython scripts use

#!/usr/bin/python
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
    sys.exit(
        load_entry_point(...)

I don't write much code using this style, but I don't see how that kind initiation can pass argv on through to the interpreter. By the time the module has been loaded and start running, the interpreter is already running.

In general it looks like ipython handles options like -i, -m, -c in basically the same way as the regular python. It may doing so with its own code, rather than delegating to the interpreter. But things like -v, -O, -t apply to the interpreter, not the REPL, and aren't handled by ipython code.

hpaulj
  • 221,503
  • 14
  • 230
  • 353