IPython、Jupyter 简单教程

内容精简自《利用Python进行数据分析·第2版》第2章 Python语法基础,IPython和Jupyter Notebooks
有一些来自其他地方的收集整理!适用于IPython shell和Jupyter QtConsole

IPython,一个强化的 Python 解释器,或 Jupyter notebooks,一个网页代码笔记本,它原先是 IPython 的一个子项目。

notebook 是 Jupyter 项目的重要组件之一,它是一个代码、文本(有标记或无标记)、数据可视化或其它输出的交互式文档。

Tab补全

IPython shell:

tab补全是字符模式,然后继续tab进入选择,可以用tab、方向键选择,回车确定

在这里插入图片描述

notebook:

tab补全是下拉框模式,可以鼠标选择

默认情况下,IPython 会隐藏下划线开头的方法和属性,比如魔术方法和内部的 “私有” 方法和属性,以避免混乱的显示(和让新手迷惑!)这些也可以 tab 补全,但是你必须首先键入一个下划线才能看到它们。如果你喜欢总是在 tab 补全中看到这样的方法,你可以 IPython 配置中进行设置。

扫描二维码关注公众号,回复: 12836631 查看本文章

还可以补全文件路径

'a:/System Volume Information/'

自省

b?
Object `b` not found.

b=[1,2,3]

b?
Type:        list
String form: [1, 2, 3]
Length:      3
Docstring:  
Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list.
The argument must be an iterable if specified.

print?
Docstring:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
Type:      builtin_function_or_method

??

使用??可以显示函数源码,仅对.py有效,python编译字节.pyc、C编译字节.pyd无效

>>>  
def add_numbers(a, b):
    """
    Add two numbers together

    Returns
    -------
    the_sum : type of arguments
    """
    return a + b
    

>>>  add_numbers??
Signature: add_numbers(a, b)
Source:   
def add_numbers(a, b):
    """
    Add two numbers together

    Returns
    -------
    the_sum : type of arguments
    """
    return a + b
File:      c:\users\jone\appdata\local\programs\python\python39\scripts\<ipython-input-26-973e9c734ef3>
Type:      function

    
>>>  add_numbers?
Signature: add_numbers(a, b)
Docstring:
Add two numbers together

Returns
-------
the_sum : type of arguments
File:      c:\users\jone\appdata\local\programs\python\python39\scripts\<ipython-input-26-973e9c734ef3>
Type:      function

搜索命名空间,匹配名字

>>>  np.*load*?
np.__loader__
np.load
np.loads
np.loadtxt

IPython vs Jupyter QtConsole

  • 比IPython shell多了半括号用法提示的功能,而提示内容既是自省的内容

  • 比IPython增加了shift+tab 反缩进功能

  • 比IPython增加了多窗口设置,以及GUI的各种快捷键

  • IPython是控制台窗口为前端,Jupyter QtConsole是纯python程序,前端是python内置的qt。因此ConEmu对Jupyter QtConsole的支持不好。如果不喜欢浏览器应用、喜欢ConEmu,那么IPython还有玩头

%run

%run a:\helloworld.py
hello world!

%run a:helloworld.py
hello world!

%run a:/helloworld.py
hello world!

确实可以补全路径,但是问题是,如果前面不加引号,他显示的则是命令,而如果前面加了引号,还需要再把引号去除

%run 'a:/helloworld.py'
Exception: File `"'a:/helloworld.py'"` not found.

通过run运行的脚本,其变量仍可以使用,很方便分析

添加-i,则脚本可以使用IPython定义的变量

%load可以加载脚本的内容

i=223261

%run -i a:/printi.py
223261

%load a:/printi.py

# %load a:/printi.py
print(i)

!

执行系统命令

In [3]: ! cmd 
Microsoft Windows [版本 10.0.17763.1757] 
(c) 2018 Microsoft Corporation。保留所有权利。                                                                                                                                             C:\Users\Jone>exit                                                                                                                                                                                             In [4]:
        
In [5]: !python a:\helloworld.py
are you ok?

In [6]:

中断运行的代码

代码运行时按 Ctrl-C,无论是 %run 或长时间运行命令,都会导致KeyboardInterrupt。这会导致几乎所有 Python 程序立即停止,除非一些特殊情况。

警告:当 Python 代码调用了一些编译的扩展模块,按 Ctrl-C 不一定将执行的程序立即停止。在这种情况下,你必须等待,直到控制返回 Python 解释器,或者在更糟糕的情况下强制终止 Python 进程。

while 1:
    pass
    
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
a:/printi.py in <module>
      1 while 1:
----> 2     pass
      3 

KeyboardInterrupt: 

确实是Crtl-C,但记得用一种情况需要用Ctrl+Pause

从剪贴板执行程序

在IPython中,可以复制粘贴整段代码到一个代码块,进而运行整段代码。

书中这里提到两个魔法函数:%paste、%cpaste,这个功能貌似已经丢弃了

键盘快捷键

很遗憾,IPython的官方文档并未详细提及快捷键,其中有提及使用第三方工具prompt_toolkit,但与内置快捷键并没有关系。

在IPython的主页有提及快捷键,jupyter的菜单中也有详细介绍。

所以就以此以及各处搜集的信息,来看看IPython的快捷键吧!

Jupyter QtConsole

Key bindings

============

The Jupyter QtConsole supports most of the basic Emacs line-oriented keybindings,

in addition to some of its own.

The keybindings themselves are:

- Enter: insert new line (may cause execution, see above). 默认的回车,会判断是否是语句块,是否语句块内回车

- Ctrl-Enter: force new line, never causes execution. 语句块内回车

- Shift-Enter: force execution regardless of where cursor is, no newline added. 立即执行当前语句块

- Up: step backwards through the history. 回翻历史语句

- Down: step forwards through the history. 前翻历史语句

- Shift-Up: search backwards through the history (like Control-r in bash). 回翻包含输入字符串的语句

- Shift-Down: search forwards through the history. 前翻包含输入字符串的语句

- Control-c: copy highlighted text to clipboard (prompts are automatically stripped). 复制选中的文本,提示符会自动删除 interrupt current kernel 中断当前内核,中断正在运行的代码

- Control-Shift-c: copy highlighted text to clipboard (prompts are not stripped). 复制选中的文本,提示符不会删除 Copy (Raw Text)

- Control-v: paste text from clipboard. 粘贴文本

- Control-z: undo (retrieves lost text if you move out of a cell with the arrows). 撤销

- Control-Shift-z: redo. 重做

- Control-o: move to ‘other’ area, between pager and terminal.

- Control-l: clear terminal. 清屏

- Control-a: go to beginning of line. 到行首Home

- Control-e: go to end of line. 到行尾End

- Control-u: kill from cursor to the begining of the line. 剪切光标前至行首的字符串

- Control-k: kill from cursor to the end of the line. 剪切光标后至行末的字符串

- Control-y: yank (paste) 粘贴 UI重做

- Control-p: previous line (like up arrow) 上一行Up

- Control-n: next line (like down arrow) 下一行Down

- Control-f: forward (like right arrow) 向前Right

- Control-b: back (like left arrow) 向后Left

- Control-d: delete next character, or exits if input is empty 删除光标后面的一个字符,当本行为空时,退出应用

- Control-h: delete previous character 删除光标前面的一个字符 BackSpace

- Alt-<: move to the beginning of the input region. Alt-Shift-, 光标跳至语句块首 - Alt-Shift-,

- Alt->: move to the end of the input region. Alt-Shift-. 光标跳至语句块末 - Alt-Shift-.

- Alt-d: delete next word. 删除光标后的单词,至空格、符号,空格带符号则删除空格,符号带空格带符号,全删…

- Alt-Backspace: delete previous word. 删除光标前的单词

- Control-.: force a kernel restart (a confirmation dialog appears). 强制重启内核,会有提示框

- Control-+: increase font size. 增加字体大小 Control-Shift-= 我用的键盘没有数字区,所以需要这样按

- Control--: decrease font size. 减小字体大小

- Control-0: reset font size. 重置字体大小

- Control-Alt-Space: toggle full screen. (Command-Control-Space on Mac OS X) 全屏,仅Mac OS X

- Control-t: new tab with new kernel 新标签页用新内核

- Control-Shift-t: new tab with same kernel 新标签页用同一内核

- Alt-t: new tab with existing kernel 新标签页用活动的内核

- Control-F4: close tab 关闭标签页

- Control-s: save to html/xhtml 保存至文件

- Control-Shift-p: print 打印

- Tab: code completion or indent 代码补全或缩进

- Shift-Tab: 反缩进

- Control-Shift-a: select cell/all 一次选中当前编辑的单元格,第二次全选当前窗口内容

- Control-Shift-m: toggle menu bar 菜单栏开关

- Control-PgUp: previous tab 上一个标签页

- Control-PgDown: next tab 下一个标签页

- Alt-r: rename window 重命名窗口

- Control-r: rename current tab 重命名当前标签页

- Control-Left: 光标到左边一个单词

- Control-Right: 光标到右边一个单词

IPython shell

做了几张图,素材来自于win10的屏幕键盘,默认主题下,窗口像素:1241x434

上图

在这里插入图片描述

在这里插入图片描述
tab补全、上下和上页下页都是翻代码,不过上下使用方便,上下页遇到代码块方便
后面还有使用组合键翻代码的,目的只为更快的输入,例如我增加了图中浅颜色的键,而图中并没改成深颜色(懒得改),是我在应用中发现这些键更常用到
在这里插入图片描述
Ctrl组合键
w 删除前面的单词至空格,包括\n
u 剪切光标至行首的字符,或删除前面的\n
k 剪切光标至行尾的字符,或删除后面的\n
a Home
e End
o 光标不动,后面输入回车
s 向前搜索包含字符的代码
l 清除输出内容,当前输入行置顶
c 清除当前输入内容;运行时中断
Up | Down PgUp | PgDown
Left | Right 光标移至 前面的单词 | 后面的单词
在这里插入图片描述
Alt组合键
BackSpace 删除前面的单词至符号
d 删除后面的单词至符号
在这里插入图片描述
# 注释掉当前的语句块

详细

- Enter: insert new line (may cause execution, see above). 默认的回车,会判断

- Ctrl-Enter: force new line, never causes execution. 语句块内回车

- Shift-Enter: force execution regardless of where cursor is, no newline added. 立即执行当前语句块

- Up: step backwards through the history. 回翻历史语句

- Down: step forwards through the history. 前翻历史语句

- Shift-Up: search backwards through the history (like Control-r in bash). 回翻包含输入字符串的语句

- Shift-Down: search forwards through the history. 前翻包含输入字符串的语句

- Control-c: copy highlighted text to clipboard (prompts are automatically stripped). 复制选中的文本,提示符会自动删除 interrupt current kernel 中断当前内核,中断正在运行的代码 当没有选中时,是删除当前语句块,选中是cmd的功能,ipython shell没有shift方向键选择字符的功能。中断是中断当前执行的语句,若一条语句执行很长时间,则需要等到该语句执行完后才中断,例如s=’ '*2**30

- Control-Shift-c: copy highlighted text to clipboard (prompts are not stripped). 复制选中的文本,提示符不会删除 Copy (Raw Text)

- Control-v: paste text from clipboard. 粘贴文本

- Control-z: undo (retrieves lost text if you move out of a cell with the arrows). 撤销

- Control-Shift-z: redo. 重做

- Control-o: move to ‘other’ area, between pager and terminal. 光标在当前位置不动,在当前位置输入回车

- Control-l: clear terminal. 清屏 清除输出,输入行置顶

- Control-a: go to beginning of line. 到行首Home

- Control-e: go to end of line. 到行尾End

- Control-u: kill from cursor to the begining of the line. 剪切光标前至行首的字符串

- Control-k: kill from cursor to the end of the line. 剪切光标后至行末的字符串

- Control-y: yank (paste) 粘贴

- Control-p: previous line (like up arrow) 上一行Up

- Control-n: next line (like down arrow) 下一行Down

- Control-f: forward (like right arrow) 向前Right

- Control-b: back (like left arrow) 向后Left

- Control-d: delete next character, or exits if input is empty 删除后面的一个字符,当本行为空时,退出应用

- Alt-<: move to the beginning of the input region. Alt-Shift-, 光标跳至语句块首莫名出现随机的之前输入过的代码,我怀疑是bug

- Alt->: move to the end of the input region. Alt-Shift-. 光标跳至语句块末

- Alt-. | Alt-Shift--: 在当前位置输入之前的语句

- Alt-d: delete next word. 删除光标后的单词,至空格、符号,空格带符号则删除空格,符号带空格带符号,全删…

- Alt-Backspace: delete previous word. 删除光标前的单词

- Control-.: force a kernel restart (a confirmation dialog appears). 强制重启内核,会有提示框

- Control-+: increase font size. 增加字体大小 Control-Shift-= 我用的键盘没有数字区,所以需要这样按

- Control--: decrease font size. 减小字体大小

- Control-0: reset font size. 重置字体大小

- Control-Alt-Space: toggle full screen. (Command-Control-Space on Mac OS X) 全屏,仅Mac OS X

- Control-t: new tab with new kernel 新标签页用新内核 光标前的字符与后面的字符交换

- Control-Shift-t: new tab with same kernel 新标签页用同一内核

- Alt-t: new tab with existing kernel 新标签页用活动的内核

- Control-F4: close tab 关闭标签页

- Control-s: save to html/xhtml 保存至文件 I-search: 匹配包含输入的字符串的最后一个语句块

- Control-Shift-p: print 打印

- Tab: code completion or indent 代码补全或缩进

- Shift-Tab: 反缩进

- Control-Shift-a: select cell/all 一次选中当前编辑的单元格,第二次全选当前窗口内容

- Control-Shift-m: toggle menu bar 菜单栏开关

- Control-PgUp: previous tab 上一个标签页

- Control-PgDown: next tab 下一个标签页

- Alt-r: rename window 重命名窗口

- Control-r: rename current tab 重命名当前标签页 I-search backward:

- Control | Alt -Left | Alt-b: 光标到左边一个单词

- Control | Alt -Right | Alt-f: 光标到右边一个单词

IPython shell 特有

上面仅划去功能,的快捷键也是IPython shell特有的,Ctrl可跟大小写,Alt跟大写无效

- Alt-Enter: cmd全屏

- Control-m | j: Enter 回车

- Control-i: Tab

- Control-w: 剪切前面的单词

- Control-Up | PgUp: 上一个语句块

- Control-Down | PgDown: 下一个语句块

- Alt-c: 从当前位置开始的这个单词,首字母大写

- Alt-/: 补全同时选中第一个单词

- Alt-l: 插入一行,是当前到下一行当前位置的单词结尾

- Ctrl-\: Unhandled exception in event loop

- Alt-\: 没有输入,但是会把视角跳至当前输入行

- Alt-u: 从当前位置开始的这个单词,全部大写

- Alt-[按键1到按键-]: Repeat 重复 效果未知

- Alt-Shift-8: 第一次是与前面字母关联的语句,很奇怪,没实际用途

- Alt-# (Alt-Shift-3): 在当前语句块中,每行开始增加#,并强制结束,专门批量注释掉语句的

查看IPython文档

官方在线文档 : 在左侧Read the Docs中,可以选择下载pdf
魔法函数

魔术命令

?:帮助文档
%lsmagic:列出当前可使用的所有魔法函数
%quickref:查看所有魔法函数的介绍,快速参考
%magic:详细的查看所有的魔法函数
object?:查看对象的详细信息,查看魔法函数的使用方法 Usage
object??:查看对象的更详细信息,查看魔法函数的源代码 Source

IPython 中特殊的命令(Python 中没有)被称作 “魔术” 命令。这些命令可以使普通任务更便捷,更容易控制 IPython 系统。魔术命令是在指令前添加百分号 % 前缀。

In [1]: %%timeit
   ...: list()
   ...:
   ...:
99.5 ns ± 0.495 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [2]: %%timeit
   ...: []
   ...:
   ...:
37.8 ns ± 0.427 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

魔术命令可以被看做 IPython 中运行的命令行。许多魔术命令有 “命令行” 选项,可以通过?查看

In [4]: %%timeit?
   ...:
   ...:
Docstring:
Time execution of a Python statement or expression

Usage, in line mode:
  %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
or in cell mode:
  %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
  code
  code...

还有很长,自己去看

魔术函数默认可以不用百分号,只要没有变量和函数名相同。这个特点被称为 “自动魔术”,可以用%automagic打开或关闭。

一些魔术函数与 Python 函数很像,它的结果可以赋值给一个变量:

In [5]: %pwd
Out[5]: 'C:\\Users\\Jone'

In [6]: d=%pwd

In [7]: d
Out[7]: 'C:\\Users\\Jone'

IPython 的文档可以在 shell 中打开,我建议你用%quickref%magic学习下所有特殊命令。命令太多,这波就不整理了,等回头再说

In [24]: %lsmagic
Out[24]:
    
Available line magics:
%alias  %alias_magic  %autoawait  %autocall  %autoindent  %automagic  %bookmark  %cd  
%cls  %colors  %conda  %config  %copy  %cpaste  %ddir  %debug  %dhist  %dirs  
%doctest_mode  %echo  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  
%load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  
%lsmagic  %macro  %magic  %matplotlib  %mkdir  %notebook  %page  %paste  %pastebin  
%pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  
%psearch  %psource  %pushd  %pwd  %pycat  %pylab  %quickref  %recall  %rehashx  
%reload_ext  %ren  %rep  %rerun  %reset  %reset_selective  %rmdir  %run  %save  %sc  
%set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  
%who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%cmd  %%debug  %%file  %%html  %%javascript  
%%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  
%%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.
  • ?
    Introduction and overview of IPython’s features (this screen).
    IPython特性的介绍和概述
  • ! cmd cmd命令
  • %pinfo | i? %store?int? 在对象后面有一个?,表示可以获取此对象的相关信息,特别是帮助信息
  • %pinfo2 | int?? 如果是两个??,则可以获取该对象更加详细的信息,比如源码。当然,有时候两者显示也一样
  • %被称作行魔法命令(line magics),只能在单个输入行上运行
  • %%被称作单元格魔法命令(cell magics),可以在多个输入行上运行
  • %lsmagic 列出所有的魔术命令
  • %store 在 notebook 之间传递变量
  • %%writefile 向文件写入单元格内容
  • %edit 生成一个.py并用编辑器打开并编辑,编辑器保存、关闭后,会运行其代码
  • %load 导入外部脚本文件
  • %mkdir 创建目录
  • %rmdir 创建目录
  • %pwd 查看当前的文件路径
  • %matplotlib inline 默认在notebook中显示图像
  • load_ext autoreload | %autoreload 2 自动导入外部模块
  • %automagic 设置输入魔法命令时是否键入%前缀 on(1)/off(0)
  • %quickref
    Display the IPython Quick Reference Card
    显示IPython快速参考
  • %magic
    Display detailed documentation for all of the available magic commands
    显示所有可用的magic命令的详细文档
  • %debug
    Enter the interactive debugger at the bottom of the last exception traceback
    在最后一次异常回溯的底部输入交互式调试器
  • %hist
    Print command input (and optionally output) history
    打印命令输入(可选输出)历史记录
  • %pdb
    Automatically enter debugger after any exception
    在出现任何异常后自动进入调试器
  • %paste
    Execute preformatted Python code from clipboard
    从剪贴板执行预格式化的Python代码
  • %cpaste
    Open a special prompt for manually pasting Python code to be executed
    打开一个特殊的提示符,用于手动粘贴要执行的Python代码
  • %reset
    Delete all variables/names defined in interactive namespace
    删除在交互式命名空间中定义的所有变量/名称
  • %page OBJECT
    Pretty-print the object and display it through a pager
    美化打印对象,并通过页面显示它
  • %run script.py
    Run a Python script inside IPython
    在IPython中运行Python脚本
  • %prun statement
    Execute statement with cProfile and report the profiler output
    使用cProfile执行语句并报告分析器输出
    计算程序中每个函数 消耗的时间
  • %time statement
    Report the execution time of a single statement
    报告单个语句的执行时间
  • %timeit statement
    Run a statement multiple times to compute an ensemble average execution time; useful for timing code with very short execution time
    多次运行一个语句,计算一个整体的平均执行时间;有用的执行时间很短的计时代码
  • %who, %who_ls, %whos
    Display variables defined in interactive namespace, with varying levels of information verbosity
    显示在交互式命名空间中定义的变量,具有不同级别的信息冗长
  • %xdel variable
    Delete a variable and attempt to clear any references to the object in the IPython internals
    删除一个变量,并尝试清除IPython内部文件中对该对象的任何引用
Debug 操作 功能
(h)elp 显示命令列表
(p)rint 打印变量
(p)retty(p)rint 打印变量,适用于链表,字典
(n)ext line 执行当前行,并进入下一行
(u)p / (d)own 在函数调用栈向上或向下移动
(s)tep 单步进入函数
c(ont(inue)) 恢复程序的执行
(r)eturn 计算语句执行时间
b(reak) n 在当前文件第 n 行设置一个断点

快速参考 by 有道

%alias:
Define an alias for a system command.
为系统命令定义别名。
%alias_magic:
Create an alias for an existing line or cell magic.
为现有的行或单元格魔法创建别名。
%autoawait:
Allow to change the status of the autoawait option.
允许更改autoawait选项的状态。
%autocall:
Make functions callable without having to type parentheses.
使函数可以调用,而不必输入圆括号。
%autoindent:
Toggle autoindent on/off (deprecated)
自动缩进开关(已弃用)
%automagic:
Make magic functions callable without having to type the initial %.
使魔术函数可调用,而不必输入初始的%。
%bookmark:
Manage IPython’s bookmark system.
管理IPython的书签系统。
%cd:
Change the current working directory.
更改当前工作目录。
%cls:
Clear screen.
清晰的屏幕。
%colors:
Switch color scheme for prompts, info system and exception handlers.
切换提示、提示系统和异常处理程序的颜色方案。
%conda:
Run the conda package manager within the current kernel.
在当前内核中运行conda包管理器。
%config:
configure IPython
配置IPython
%copy:
Alias for !copy
别名“复制!”
%cpaste:
Paste & execute a pre-formatted code block from clipboard.
从剪贴板粘贴并执行预格式化的代码块。
%ddir:
Alias for !dir /ad /on
别名”!dir /广告/ ’
%debug:
Activate the interactive debugger.
激活交互式调试器。
%dhist:
Print your history of visited directories.
打印您访问的目录的历史。
%dirs:
Return the current directory stack.
返回当前目录堆栈。
%doctest_mode:
Toggle doctest mode on and off.
打开和关闭doctest模式。
%echo:
Alias for !echo
别名“!回声”
%ed:
Alias for %edit.
别名“%编辑”。
%edit:
Bring up an editor and execute the resulting code.
打开编辑器并执行生成的代码。
%env:
Get, set, or list environment variables.
获取、设置或列出环境变量。
%gui:
Enable or disable IPython GUI event loop integration.
启用或禁用IPython GUI事件循环集成。
%hist:
Alias for %history.
别名“%历史”。
%history:
Print input history (_i variables), with most recent last.
打印输入历史(_i<n个>变量),包含最近的最后一个。
%killbgscripts:
Kill all BG processes started by %%script and its family.
杀死所有由%%script及其家族启动的BG进程。
%ldir:
Alias for !dir /ad /on
别名”!dir /广告/ ’
%load:
Load code into the current frontend.
将代码加载到当前前端。
%load_ext:
Load an IPython extension by its module name.
通过模块名加载一个IPython扩展。
%loadpy:
Alias of %load
别名“%负载”
%logoff:
Temporarily stop logging.
暂时停止日志记录。
%logon:
Restart logging.
重新启动日志记录。
%logstart:
Start logging anywhere in a session.
开始记录会话中的任何位置。
%logstate:
Print the status of the logging system.
打印日志系统的状态。
%logstop:
Fully stop logging and close log file.
完全停止日志记录并关闭日志文件。
%ls:
Alias for !dir /on
别名”!dir /“
%lsmagic:
List currently available magic functions.
列出目前可用的魔术函数。
%macro:
Define a macro for future re-execution. It accepts ranges of history,
定义一个宏以备将来再次执行。它接受历史的范围,
%magic:
Print information about the magic function system.
打印魔术功能系统的信息。
%matplotlib:
Set up matplotlib to work interactively.
设置matplotlib以交互方式工作。
%mkdir:
Alias for !mkdir
别名“mkdir !”
%notebook:
Export and convert IPython notebooks.
导出和转换IPython笔记本。
%page:
Pretty print the object and display it through a pager.
Pretty打印对象并通过寻呼机显示。
%paste:
Paste & execute a pre-formatted code block from clipboard.
从剪贴板粘贴并执行预格式化的代码块。
%pastebin:
Upload code to dpaste’s paste bin, returning the URL.
上传代码到dpaste的粘贴bin,返回URL。
%pdb:
Control the automatic calling of the pdb interactive debugger.
控制pdb交互式调试器的自动调用。
%pdef:
Print the call signature for any callable object.
打印任何可调用对象的调用签名。
%pdoc:
Print the docstring for an object.
打印对象的文档字符串。
%pfile:
Print (or run through pager) the file where an object is defined.
打印(或浏览)定义对象的文件。
%pinfo:
Provide detailed information about an object.
提供对象的详细信息。
%pinfo2:
Provide extra detailed information about an object.
提供关于对象的额外详细信息。
%pip:
Run the pip package manager within the current kernel.
在当前内核中运行pip包管理器。
%popd:
Change to directory popped off the top of the stack.
更改到目录从堆栈顶部弹出。
%pprint:
Toggle pretty printing on/off.
打开/关闭漂亮的打印。
%precision:
Set floating point precision for pretty printing.
设置浮点精度为漂亮的打印。
%prun:
Run a statement through the python code profiler.
通过python代码分析器运行一条语句。
%psearch:
Search for object in namespaces by wildcard.
通过通配符在名称空间中搜索对象。
%psource:
Print (or run through pager) the source code for an object.
打印(或浏览)一个对象的源代码。
%pushd:
Place the current dir on stack and change directory.
将当前目录放置在堆栈和更改目录上。
%pwd:
Return the current working directory path.
返回当前工作目录路径。
%pycat:
Show a syntax-highlighted file through a pager.
通过寻呼机显示语法突出显示的文件。
%pylab:
Load numpy and matplotlib to work interactively.
加载numpy和matplotlib以交互方式工作。
%quickref:
Show a quick reference sheet
出示一份快速参考表
%recall:
Repeat a command, or get command to input line for editing.
重复一个命令,或获取命令到输入行进行编辑。
%rehashx:
Update the alias table with all executable files in P A T H . 用 PATH. 用 PATH.PATH中的所有可执行文件更新别名表。
%reload_ext:
Reload an IPython extension by its module name.
通过模块名重新加载IPython扩展。
%ren:
Alias for !ren
别名“任!”
%rep:
Alias for %recall.
别名“%回忆”。
%rerun:
Re-run previous input
重新运行之前的输入
%reset:
Resets the namespace by removing all names defined by the user, if
通过删除用户定义的所有名称来重置命名空间,如果
%reset_selective:
Resets the namespace by removing names defined by the user.
通过删除用户定义的名称来重置命名空间。
%rmdir:
Alias for !rmdir
别名“删除文件夹!”
%run:
Run the named file inside IPython as a program.
在IPython中以程序的形式运行命名文件。
%save:
Save a set of lines or a macro to a given filename.
将一组行或宏保存到给定的文件名。
%sc:
Shell capture - run shell command and capture output (DEPRECATED use !).
Shell捕获-运行Shell命令并捕获输出(反对使用!)
%set_env:
Set environment variables. Assumptions are that either “val” is a
设置环境变量。假设任意一个“val”是a
%store:
Lightweight persistence for python variables.
python变量的轻量级持久性。
%sx:
Shell execute - run shell command and capture output (!! is short-hand).
执行Shell命令并捕获输出(!!)速记)。
%system:
Shell execute - run shell command and capture output (!! is short-hand).
执行Shell命令并捕获输出(!!)速记)。
%tb:
Print the last traceback.
打印最后一次回溯。
%time:
Time execution of a Python statement or expression.
Python语句或表达式的定时执行。
%timeit:
Time execution of a Python statement or expression
Python语句或表达式的定时执行
%unalias:
Remove an alias
删除一个别名
%unload_ext:
Unload an IPython extension by its module name.
通过模块名卸载IPython扩展。
%who:
Print all interactive variables, with some minimal formatting.
以最小的格式打印所有交互变量。
%who_ls:
Return a sorted list of all interactive variables.
返回所有交互变量的排序列表。
%whos:
Like %who, but gives some extra information about each variable.
像%who一样,但是给出了关于每个变量的一些额外信息。
%xdel:
Delete a variable, trying to clear it from anywhere that
删除一个变量,试图从任何地方清除它
%xmode:
Switch modes for the exception handlers.
切换异常处理程序的模式。
%%!:
Shell execute - run shell command and capture output (!! is short-hand).
执行Shell命令并捕获输出(!!)速记)。
%%HTML:
Alias for %%html.
别名“% % html”。
%%SVG:
Alias for %%svg.
别名“% % svg”。
%%bash:
%%bash script magic
% %魔法bash脚本
%%capture:
run the cell, capturing stdout, stderr, and IPython’s rich display() calls.
运行单元格,捕获stdout、stderr和IPython的富display()调用。
%%cmd:
%%cmd script magic
% %魔法cmd脚本
%%debug:
Activate the interactive debugger.
激活交互式调试器。
%%file:
Alias for %%writefile.
别名“% % writefile”。
%%html:
Render the cell as a block of HTML
将单元格呈现为HTML块
%%javascript:
Run the cell block of Javascript code
运行Javascript代码的单元格
%%js:
Run the cell block of Javascript code
运行Javascript代码的单元格
%%latex:
Render the cell as a block of latex
将细胞渲染成一块乳胶
%%markdown:
Render the cell as Markdown text block
将单元格呈现为Markdown文本块
%%perl:
Run cells with perl in a subprocess.
在子进程中使用perl运行单元格。
%%prun:
Run a statement through the python code profiler.
通过python代码分析器运行一条语句。
%%pypy:
Run cells with pypy in a subprocess.
在子进程中使用pypy运行单元格。
%%python:
Run cells with python in a subprocess.
在子进程中使用python运行单元格。
%%python2:
Run cells with python2 in a subprocess.
在子进程中使用python2运行单元格。
%%python3:
Run cells with python3 in a subprocess.
在子进程中使用python3运行单元格。
%%ruby:
Run cells with ruby in a subprocess.
在子进程中使用ruby运行单元格。
%%script:
Run a cell via a shell command
通过shell命令运行单元格
%%sh:
Run cells with sh in a subprocess.
在子进程中使用sh运行单元格。
%%svg:
Render the cell as an SVG literal
将单元格呈现为SVG文本
%%sx:
Shell execute - run shell command and capture output (!! is short-hand).
执行Shell命令并捕获输出(!!)速记)。
%%system:
Shell execute - run shell command and capture output (!! is short-hand).
执行Shell命令并捕获输出(!!)速记)。
%%time:
Time execution of a Python statement or expression.
Python语句或表达式的定时执行。
%%timeit:
Time execution of a Python statement or expression
Python语句或表达式的定时执行

集成 Matplotlib

IPython 在分析计算领域能够流行的原因之一是它非常好的集成了数据可视化和其它用户界面库,比如 matplotlib。

In [9]: %matplotlib
ModuleNotFoundError: No module named 'matplotlib'

但我试了下,并没有,大概是我没有安装这个模块的原因

猜你喜欢

转载自blog.csdn.net/jhsxy2005/article/details/114298570