Robert G
Robert G

Reputation: 581

Unable to set IPython prompt

I'm running ipython with the following:

c:\python27\scripts\ipython

I'm trying to restore old Python prompt behaviour (">>>") for some reasons.

I've tried to search internet extensively for that purpose, to no avail.

Then I reached for IPython documentation, which turned out to be confusing and unhelpful. According to http://ipython.readthedocs.io/en/stable/config/details.html

To set the new prompt, assign it to the prompts attribute of the IPython shell:

In [2]: ip = get_ipython()
...: ip.prompts = MyPrompt(ip)

/home/bob >>> # it works

I get the exception that get_ipython is undefined:

[TerminalIPythonApp] ERROR | Exception while loading config file C:\Users\xxx\.ipython\profile_default\ipython_config.py
Traceback (most recent call last):
File "c:\python27\lib\site-packages\traitlets\config\application.py", line 562, in _load_config_files
    config = loader.load_config()
  File "c:\python27\lib\site-packages\traitlets\config\loader.py", line 457, in load_config
    self._read_file_as_dict()
  File "c:\python27\lib\site-packages\traitlets\config\loader.py", line 489, in _read_file_as_dict
    py3compat.execfile(conf_filename, namespace)
  File "c:\python27\lib\site-packages\ipython_genutils\py3compat.py", line 278, in execfile
    exec(compiler(scripttext, filename, 'exec'), glob, loc)
  File "C:\Users\rgomulk\.ipython\profile_default\ipython_config.py", line 9, in <module>
    ip = get_ipython()
NameError: name 'get_ipython' is not defined
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

(With the following complete code in my ipython_config.py:

from IPython.terminal.prompts import Prompts, Token

class MyPrompt(Prompts):
    def in_prompt_tokens(self, cli=None):
        return [(Token.Prompt, ' >>>')]

ip = get_ipython()
ip.prompts = MyPrompt(ip)

After next round of googling I've added the following line to the config:

from IPython import get_ipython

This time the result was different:

[TerminalIPythonApp] ERROR | Exception while loading config file C:\Users\xxx\.ipython\profile_default\ipython_config.py
Traceback (most recent call last):
  File "c:\python27\lib\site-packages\traitlets\config\application.py", line 562, in _load_config_files
    config = loader.load_config()
  File "c:\python27\lib\site-packages\traitlets\config\loader.py", line 457, in load_config
    self._read_file_as_dict()
  File "c:\python27\lib\site-packages\traitlets\config\loader.py", line 489, in _read_file_as_dict
    py3compat.execfile(conf_filename, namespace)
  File "c:\python27\lib\site-packages\ipython_genutils\py3compat.py", line 278, in execfile
    exec(compiler(scripttext, filename, 'exec'), glob, loc)
  File "C:\Users\rgomulk\.ipython\profile_default\ipython_config.py", line 11, in <module>
    ip.prompts = MyPrompt(ip)
AttributeError: 'NoneType' object has no attribute 'prompts'

So the question is twofold: 1. How can I actually set the prompt/restore old prompt behaviour? 2. Why doesn't the code from IPython documentation work? Is that a bug in implementation or documentation?

IPython version and other versions already given in IPython output.

Regards, Robert

Upvotes: 2

Views: 1664

Answers (2)

Robert G
Robert G

Reputation: 581

Another possibility - less generic - is to use predefined Prompt class:

from IPython.terminal.prompts import ClassicPrompts

c = get_config()

c.TerminalInteractiveShell.prompts_class = ClassicPrompts

Upvotes: 3

Robert G
Robert G

Reputation: 581

After extensive search (it was really tedious and others are confused as well, especially from distinguishing between startup and configuration scripts) I've found this page: Jupyter prompts

Which led to (working) solution:

from IPython.terminal.prompts import Prompts
from pygments.token import Token

class MyPrompt(Prompts):
    def in_prompt_tokens(self, cli=None):
        return [(Token.Prompt, '>>> ')]

c.TerminalInteractiveShell.prompts_class = MyPrompt

(Please notice the lack of what seems to be required by official documentation Official IPython docs:

The files typically start by getting the root config object:

c = get_config()

Regards,

Robert

Upvotes: 4

Related Questions