内省
在变量的前面或后面加上一个问号(?)就可以将有关该对象的一些通用信息显示出来:
In [7]: b = [1, 2, 3]
In [8]: b?
Type: list
String form: [1, 2, 3]
Length: 3
Docstring:
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
如果该对象是一个函数或实例方法,则其docstring也会被显示出来:
In [10]: def add_numbers(a, b):
....: """
....: Add two numbers together
....: Returns
....: ------
....: the_sum: type of arguments
....: """
....: return a + b
....:
In [11]: add_numbers?
Type: function
String form: <function add_numbers at 0x10418c6e0>
File: /Users/lgt/<ipython-input-10-c944397568a6>
Definition: add_numbers(a, b)
Docstring:
Add two numbers together
Returns
------
the_sum: type of arguments
使用??还将显示出该函数的源代码(如果可能的话):
In [12]: add_numbers??
Type: function
String form: <function add_numbers at 0x10418c6e0>
File: /Users/lgt/<ipython-input-10-c944397568a6>
Definition: add_numbers(a, b)
Source:
def add_numbers(a, b):
"""
Add two numbers together
Returns
------
the_sum: type of arguments
"""
return a + b
而?甚至可以用于正则匹配:
In [13]: np.*load?
np.load
np.pkgload
%run命令
在IPython会话环境中,所有文件都可以通过%run命令当做Python程序来运行:
lgtdeMacBook-Pro:~ lgt$ cat ipython_script_test.py
def f(x, y, z):
return (x + y) / z
a = 5
b = 6
c = 7.5
result = f(a, b, c)
我们通过%run来运行:
In [1]: %run ipython_script_test.py
脚本是在空的命名空间中运行的,所以其行为应该跟在标准命令行环境中执行时一样.此后,该文件所定义的全部变量(还有各种import, 函数和全局变量)就可以在当前IPython shell中访问.
In [2]: c
Out[2]: 7.5
In [3]: result
Out[3]: 1.4666666666666666
异常和跟踪
如果%run某段脚本或执行某条语句时发生了异常,IPython默认会输出整个调用栈跟踪(trackback),其中还会附上调用栈各点附近的几行代码作为上下文参考:
In [5]: %run /Users/lgt/pydata-book/ch03/ipython_bug.py
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
/Users/lgt/pydata-book/ch03/ipython_bug.py in <module>()
13 throws_an_exception()
14
---> 15 calling_things()
/Users/lgt/pydata-book/ch03/ipython_bug.py in calling_things()
11 def calling_things():
12 works_fine()
---> 13 throws_an_exception()
14
15 calling_things()
/Users/lgt/pydata-book/ch03/ipython_bug.py in throws_an_exception()
7 a = 5
8 b = 6
----> 9 assert(a + b == 10)
10
11 def calling_things():
AssertionError:
魔术命令
魔术命令是以百分号%为前缀的命令.例如%timeit可以检测任意Python语句的执行时间:
In [8]: import numpy as np
In [9]: a = np.random.randn(100, 100)
In [10]: %timeit np.dot(a, a)
10000 loops, best of 3: 86 µs per loop
我们可以通过?来查看命令行选项.
下面是几个常用的魔术命令:
命令 | 说明 |
%quickref | 显示IPython的快速参考 |
%magic | 显示所有魔术命令的详细文档 |
%debug | 从最新的异常跟踪的底部进入交互式调试器 |
%hist | 打印命令的输入(可选输出)历史 |
%pdb | 在异常发生后自动进入调试器 |
%paste | 执行剪贴板中的Python代码 |
%cpaste | 打开一个特殊提示符以便手工粘贴待执行的Python代码 |
%reset | 删除interactive命令空间中的全部变量/名称 |
%page OBJECT | 通过分页器打印输出OBJECT |
%run script.py | 在IPython中执行一个Python脚本文件 |
%prun statement | 通过cProfile执行statement,并打印分析器的输出结果 |
%time statement | 报告statement的执行时间 |
%timeit statement | 多次执行以计算平均执行时间 |
%who, %who_is, %whos | 显示interactive命名空间中定义的变量,信息级别/冗余度可变 |
%xdel variable | 删除variable,并尝试清除其在IPython中的对象上的一切引用 |
记录输入和输出
IPython能够记录整个控制台会话,包括输入输出.执行%logstart即可开始记录日志.
In [15]: %logstart
Activating auto-logging. Current session state plus future input saved.
Filename : ipython_log.py
Mode : rotate
Output logging : False
Raw input log : False
Timestamping : False
State : active
与操作系统交互
命令 | 说明 |
!cmd | 在系统shel中执行cmdl |
output = !cmd args | 执行cmd,并将stdout存放在output中 |
%alias alias_name cmd | 为系统shell命令定义别名 |
%bookmark | 使用IPython的目录书签系统 |
%cd directory | 将系统工作目录更改为directory |
%pwd | 返回系统的当前工作目录 |
%pushed directory | 将当前目录入栈,并转向目标目录 |
%popd | 弹出栈顶目录,并转向该目录 |
%dirs | 返回一个函数当前目录栈的列表 |
%dhist | 打印目录访问历史 |
%env | 以dic形式返回系统环境变量 |
shell命令和别名
!cmd可以执行cmd命令:
In [17]: !python
Python 2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
而%alias用来定义别名:
In [18]: %alias ll ls -l
In [19]: ll /usr
total 8
drwxr-xr-x 5 root wheel 170 9 10 2014 X11
lrwxr-xr-x 1 root wheel 3 2 22 17:01 X11R6 -> X11
drwxr-xr-x 1050 root wheel 35700 7 4 12:41 bin
drwxr-xr-x 257 root wheel 8738 7 4 12:41 include
drwxr-xr-x 271 root wheel 9214 7 4 12:42 lib
drwxr-xr-x 170 root wheel 5780 7 4 12:44 libexec
drwxrwxr-x 22 root admin 748 7 3 17:30 local
drwxr-xr-x 245 root wheel 8330 7 4 12:42 sbin
drwxr-xr-x 44 root wheel 1496 4 18 00:35 share
drwxr-xr-x 4 root wheel 136 2 22 16:56 standalone
目录书签系统
我们可以为经常使用的目录设置书签(自动持久化):
In [20]: %bookmark ws /Users/lgt/Workspace
In [21]: cd ws
(bookmark:ws) -> /Users/lgt/Workspace
/Users/lgt/Workspace
In [22]: pwd
Out[22]: u'/Users/lgt/Workspace'