首页 > 代码库 > ipython
ipython
ipython为什么这么多人用?
很大的原因在于它有以下几大特性:
1. 相较于python解释器来说,它能通过 tab键来代码补全
2.也有可以在浏览器上直接跑脚本的,基于网页动态交互的 notebook
3.还有强大的内省(Introspection)功能:
可以在对象后加“?”,就可以看到所有有关对象的相关信息,比如对象类型,对象的有关类的 文档等等
在对像后加"??“,可以直接跳转到引用到的源代码
可以用 通配符"object.*load*?"查找 空间内的namespace
4.用%run命令,运行你所在位置的 python脚本,<Ctrl-C>中断
5.可以从粘贴板上执行代码(<Ctrl-Shift-V>),以及%paste 和 %cpaste命令,也有IDE有关它的扩展
6.快捷键,Ctrl-a,Ctrl-b,Ctrl-e,Ctrl-f,Ctrl-u,Ctrl-k
Command Description
Ctrl-P or up-arrow Search backward in command history for commands starting with currently-entered text
Ctrl-N or down-arrow Search forward in command history for commands starting with currently-entered text
Ctrl-R Readline-style reverse history search (partial matching)
Ctrl-Shift-V Paste text from clipboard
Ctrl-C Interrupt currently-executing code
Ctrl-A Move cursor to beginning of line
Ctrl-E Move cursor to end of line
Ctrl-K Delete text from cursor until end of line
Ctrl-U Discard all text on current line
Ctrl-F Move cursor forward one character
Ctrl-B Move cursor back one character
Ctrl-L Clear screen
7.%xmode命令看到有关程序错误的更多信息
8.%timeit命令分析每条语句的执行速度
9.%reset 清空用户的所有定义
Command Description
%quickref Display the IPython Quick Reference Card
%magic Display detailed documentation for all of the available magic commands
%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 pre-formatted Python code from clipboard
%cpaste Open a special prompt for manually pasting Python code to be executed
%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
%prun statement Execute statement with cProfile and report the profiler output
%time statement Report the execution time of single statement
%timeit statement Run a statement multiple times to compute an emsemble 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
%gui
10.还有 丰富的Qt-based控制台(ipython qtconsole --pylab=inline)
11.Matplotlib Integration and Pylab Mode($ ipython --pylab):与Matplotlib有更好的交互
12.命令历史,可以对命令进行操作(<Ctrl-R>:搜索历史记录,),可以将输入的命令存入log文件中。
13.可以和你的系统交互(用类似 linux的命令)
14.可以收藏 系统文件价,下次可以直接跳转到相应目录
15.开发工具:
可交互debugger(%pdb):
Command Action
h(elp) Display command list
help command Show documentation for command
c(ontinue) Resume program execution
q(uit) Exit debugger without executing any more code
b(reak) number Set breakpoint at number in current file
b path/to/file.py:number Set breakpoint at line number in specified file
s(tep) Step into function call
n(ext) Execute current line and advance to next line at current level
u(p) / d(own) Move up/down in function call stack
a(rgs) Show arguments for current function
debug statement Invoke statement statement in new (recursive) debugger
l(ist) statement Show current position and context at current level of stack
w(here) Print full stack trace with context at current position
16.测运行函数花费的时间(%prun -l 7 -s cumulative run_experiment()),不同于cprofile接py文件($ python -m cProfile -s cumulative cprof_example.py)
17.一行一行的剖析函数(需要添加line_profiler库,使用如下%lprun -f add_and_sum add_and_sum(x, y))
注意:
1.import函数后,修改了,需要reload(function)
ipython