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

Question:

I would like to add some commandline options to a python launch code in order to actually invoke an ipython shell. How do I do that?

Asked By: Shwouchk

||

Answers:

It is not entirely clear what you mean by “a python launch code”; I assume this refers to the shell code you use to launch Python.

On Unix, you could use alias to substitute one command for another:

aix@aix:~$ alias python=ipython
aix@aix:~$ python
Enthought Python Distribution -- http://www.enthought.com

Python 2.7.1 |EPD 7.0-2 (64-bit)| (r271:86832, Nov 29 2010, 13:51:37) 
Type "copyright", "credits" or "license" for more information.

IPython 0.10.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: 

If this is not what you want, please clarify your question.

Answered By: NPE

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.

Answered By: Sven Marnach

I think you mean something like python C:Python27Scriptsipython-script.py

Answered By: matlabdbuser

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

Answered By: rgtk

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.

Answered By: Heberto Mayorquin

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.

Answered By: colidyre