jez
jez

Reputation: 15349

starting IPython using Python code

I'm in a Python 2.7 shell on Windows and, because I'm going crazy without tab completion and ? documentation, I want to start an IPython shell from there.

For various (really tedious) reasons I can't launch IPython directly via Scripts\ipython.exe. I just want pure-Python code that will take me to IPython. I've searched-for and found the following:

from IPython.terminal.ipapp import TerminalIPythonApp as t;
t.instance().initialize()

or

from IPython.terminal.ipapp import TerminalInteractiveShell as t;
t.instance()

...and in both of these cases I end up in some kind of IPython shell, but in both it's not what I recognize as a "normal" IPython session: for example it lacks colour, lacks the [1] next to In and Out, and lacks the ability to use ? and ?? to view docstrings.

Is there a way to get to the "normal"-looking and -behaving IPython shell from Python, without having to come out of Python and launch directly from the .exe? In principle I imagine there must be a pure-Python (and hence cross-platform, not even Windows-specific) way of doing it, but I'm googling in circles trying to find it.

Upvotes: 0

Views: 178

Answers (2)

jez
jez

Reputation: 15349

By following the lead of Scripts\ipython-script.py and looking at what pkg_resources.load_entry_point returns in various cases, I ended up with the following script, which seems to cover all my bases:

import sys
import IPython
try:
    from IPython import start_ipython as entry_point
except ImportError:
    try:
        from IPython.terminal.ipapp import launch_new_instance as entry_point
    except ImportError:
        try:
            from IPython.frontend.terminal.ipapp import launch_new_instance as entry_point
        except ImportError:
            try:
                from pkg_resources import load_entry_point
                entry_point = load_entry_point( 'ipython', 'console_scripts', 'ipython' )
            except ImportError:
                try:
                    from pkg_resources import run_script
                    def entry_point(): run_script( 'ipython', 'ipython' ); return 0
                except ImportError:
                    raise ImportError( 'run out of options for launching IPython version %s under Python version %s' % ( IPython.__version__, sys.version ) )
sys.exit( entry_point() )

Upvotes: 0

Chemik
Chemik

Reputation: 1479

This is from ipython starter script in linux.

from IPython import start_ipython
start_ipython()

Upvotes: 1

Related Questions