ipython qtconsole --pylab=inline
ipython --pylab
matplotlib
和numpy
库.?
可打印关于该对象的一些通用信息。In [3]: a = [1,3,5]
In [4]: a?
Type: list
String form: [1, 3, 5]
Length: 3
Docstring:
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
??
,可打印该对象的更详细的信息。如对象为函数,则会打印该函数的源码.In [9]: test??
Signature: test()
Source:
def test():
print('hello world!')
File: g:\tmp\dataanalysis\7-b10b50e160d7>
Type: function
*
加上?
搜索ipython的命名空间,显示匹配的变量.In [18]: plt.im*?
plt.imread
plt.imsave
plt.imshow
%run xxx.py
: 用于执行指定的python文件,脚本在一个空的命名空间中运行,执行完后所有该脚本中定义的变量,都可以在ipython shell中访问. 也可通过%run -p xxx.py
启动带cProfile的方式执行python文件.
%prun statement
: 通过cProfile执行statement,病打印分析器的输出结果. 如:
In [1]: def test(a):
b = 20 / a
print(b)
In [2]: %prun test(2)
10.0
5 function calls in 0.000 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 {built-in method builtins.print}
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 44-9465b62ce9a6>:1(test)
1 0.000 0.000 0.000 0.000 :1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
lprun
: 用于针对行做微观的代码执行性能分析, 但需要启用line_profiler
这个ipython扩展支持。此处不详细描述。
%hist
: 显示历史命令.
%reset
: 清空交互shell的所有命名空间.
%xdel var
: 删除var, 并尝试清除其在ipython中的对象上的一切引用.
%time statement
: 报告statement执行时间
%timeit statement
: 多次执行statement并计算平均执行时间,对于执行非常小的代码时非常有用. 如:
In [49]: def test(a):
....: b = 20 / a
....: return b
....:
In [50]: %time test(2)
Wall time: 0 ns
Out[50]: 10.0
In [51]: %timeit test(2)
The slowest run took 42.41 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 84.3 ns per loop
%debug
: 当执行脚本异常时,通过该命令可从最新的异常底部进入交互式调试器.
%pdb
: 开启后, 异常发生即自动进入调试器模式,再次执行即关闭.
%cpaste
: 打开一个特殊提示符一边手工粘贴待执行的代码. 如:
In [53]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:def test(a):
: b = 20 / a
: return b
:
%bookmark name dir
: 将目录dir保存为名称为name的目录书签,下次打开ipython后可通过cd name
直接进入到该目录. 如:
In [102]: %bookmark work G:\tmp\DataAnalysis
In [103]: cd work
(bookmark:work) -> G:\tmp\DataAnalysis
G:\tmp\DataAnalysis
%dhist
: 打印目录访问历史
ctrl+r
: 增量搜索历史命令
ctrl+L
: 清屏
ctrl+shift+v
: 执行剪贴板中的python代码
其他光标导航快捷键和bash基本相同.
h(elp)
: 显示命令列表
c(ontinue)
: 恢复程序的执行
q(uit)
: 退出pdb
b(reak) num
: 在当前文件的第num行断点
b(reak) xxx.py:num
: 在指定文件的第num行断点
s(tep)
: 单步进入
n(ext)
: 单步执行
u(p)/d(own)
: 在函数栈中上下移动
a(rgs)
: 显示当前函数参数
debug statement
: 在新的(递归)调试器中调用语句statement
l(ist) statement
: 显示当前行或指定行的上下文参考
w(here)
: 打印当前位置的完整栈跟踪, 包含上下文参考代码
_
和__
变量中.!cmd
可执行系统命令, 且可通过类似a = !cmd
接受命令返回的结果. 如:In [77]: !ls
01.MovieLens1M 1.jpg ml-latest-small.zip
In [78]: a = !ls
In [79]: a
Out[79]: ['01.MovieLens1M', '1.jpg', 'ml-latest-small.zip']
reload(lib)
: 该函数用于重新加载lib dreload(lib)
: 该函数用于深度(递归)重新加载lib及其所有的依赖项