0

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.

1
  • The "tedious reasons" are related to the fact that, because of third-party-package dependency hell, I have multiple Python distributions: some have IPython<=0.13, some are more modern. Two are Python 2.7.x, but one is 3.2 and one is even 2.5.4 (ugh). One even lacks setuptools and (for even more tedious reasons) I want to keep it that way. On my PsychoPy distro (Python 2.7.3, with setuptools) ipython.exe just fails silently. Commented Feb 5, 2015 at 0:32

2 Answers 2

1

This is from ipython starter script in linux.

from IPython import start_ipython
start_ipython()
Sign up to request clarification or add additional context in comments.

1 Comment

Looks like this is indeed the best and most "IPythonic" answer provided one's version of the package is modern enough. That's not always the case for me, but I didn't tell you that in the question, and your answer put me on the right track to cover my other bases---thanks!
0

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() )

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.