How can I launch ipython from shell, by running 'python ...'?

To start IPython shell directly in Python:

from IPython import embed

a = "I will be accessible in IPython shell!"

embed()

Or, to simply run it from command line:

$ python -c "from IPython import embed; embed()"

embed will use all local variables inside shell.

If you want to provide custom locals (variables accessible in shell) take a look at IPython.terminal.embed.InteractiveShellEmbed


You can first install IPython for your specific version and then start Python with module name, e.g.:

$ python3.7 -m pip install IPython
$ python3.7 -m IPython

Python 3.7.7 (default, Mar 10 2020, 17:25:08) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

So you can even have multiple Python versions installed and start an IPython interpreter for each version separately. A next step for convenience would be to alias the command, e.g. in .bashrc:

alias ipython3.7='python3.7 -m IPython'

Then you can easily start IPython for the specific version(s):

$ ipython3.7

Python 3.7.7 (default, Mar 10 2020, 17:25:08) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

See also https://github.com/ipython/ipython#development-and-instant-running.


To do exactly what you asked for, i.e. add command line options to a python invocation to actually invoke IPython, you can do this:

python -c 'import subprocess; subprocess.call("ipython")'

I can't imagine, though, any circumstances where this would be useful.


Maybe an option is just to embed ipython in your code like this

def some_function():
    some code

    import IPython
    IPython.embed()

When you run the function in some code it will launch and ipython terminal whose scope is the one of the function from where it was called.