默認(rèn)情況下,IPython的默認(rèn)提示符格式是如下:
$ ipython
Python 3.6.1 (default, Apr 6 2017, 11:37:13)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import numpy as np
In [2]: a = np.arange(15).reshape(3, 5)
In [3]: a
Out[3]:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
是In [1]:
加上行號的格式程帕。輸出代碼也是同樣的格式Out[3]:
尽超。
有的時(shí)候我們在寫測試代碼,希望把它復(fù)制到博客里面的時(shí)候愧哟,這種格式看起來就不那么美觀,更希望它是Python的默認(rèn)提示符>>>
風(fēng)格哼蛆。
$ python
Python 3.6.1 (default, Apr 6 2017, 11:37:13)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a = np.arange(15).reshape(3, 5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
那我們該如何配置呢? 具體操作如下(文末更新有最簡單的方式):
首先創(chuàng)建IPython的自定義配置文件
$ ipython profile create
[ProfileCreate] Generating default config file: '~/.ipython/profile_default/ipython_config.py'
[ProfileCreate] Generating default config file: '~/.ipython/profile_default/ipython_kernel_config.py'
可以看到在HOME目錄下: 多了兩個(gè)配置文件
我們修改~/.ipython/profile_default/ipython_config.py
文件, 在文件的最底部,
加入如下代碼:
$ tail -n 12 ~/.ipython/profile_default/ipython_config.py
from IPython.terminal.prompts import Prompts, Token
import os
class MyPrompt(Prompts):
def in_prompt_tokens(self, cli=None):
return [(Token.Prompt, '>>> ')]
def out_prompt_tokens(self):
return []
c.TerminalInteractiveShell.prompts_class = MyPrompt
重新打開IPython, 實(shí)現(xiàn)效果如下:
$ ipython
Python 3.6.1 (default, Apr 6 2017, 11:37:13)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
>>> import numpy as np
>>> a = np.arange(15).reshape(3, 5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
參考:
http://ipython.readthedocs.io/en/stable/config/details.html#custom-prompts
更新
在看了Ipython的代碼IPython/terminal/prompts.py
時(shí)蕊梧,發(fā)現(xiàn)它已經(jīng)定義好了經(jīng)典提示符的風(fēng)格ClassicPrompts
。不需要我們再自定義實(shí)現(xiàn)了腮介,直接在配置文件~/.ipython/profile_default/ipython_config.py
里配置使用它即可
c.TerminalInteractiveShell.prompts_class = 'IPython.terminal.prompts.ClassicPrompts'