Python 3.6.4 (v3.6.4:d48eceb, Dec 192017, 06:54:40) [MSC v.190064 bit (AMD64)] Type 'copyright', 'credits'or'license'for more information IPython 6.2.1 -- An enhanced Interactive Python. Type '?'for help.
In [4]: def(a,b): ...: """ ...: Add two numbers together ...: Returns ...: ----------------------- ...: the sum: type of arguments ...: """ ...: return a+b ...: ...:
In [5]: add_number? Signature: add_number(a, b) Docstring: Add two numbers together Returns ----------------------- the sum: type of arguments File: d:python<ipython-input-4-7144b04645ed> Type: function
使用??还将显示源代码:
1 2 3 4 5 6 7 8 9 10 11 12 13
In [6]: add_number?? Signature: add_number(a, b) Source: def(a,b): """ Add two numbers together Returns ----------------------- the sum: type of arguments """ return a+b File: d:python<ipython-input-4-7144b04645ed> Type: function
In [16]: %cpaste Pasting code; enter '--' alone on the line to stop or use Ctrl-D. :deff(x, y, z): : return (x+y) /z : :a = 1 :b = 2 :c = 3 :result = f(a, b, c) :--
In [17]: %run ipython_bug.py --------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) D:Pythonipythonipython_bug.py in <module>() 5 b = 2 6 c = 0 ----> 7 result = f(a, b, c)
D:Pythonipythonipython_bug.py in f(x, y, z) 1deff(x, y, z): ----> 2 return (x+y) /z 3 4 a = 1 5 b = 2
In [44]: %reset? Docstring: Resets the namespace by removing all names defined by the user, if called without arguments, or by removing some types of objects, such as everything currently in IPython's In[] and Out[] containers (see the parameters for details). Parameters ---------- -f : force reset without asking for confirmation. -s : 'Soft' reset: Only clears your namespace, leaving history intact. References to objects may be kept. By default (without this option), we do a 'hard' reset, giving you a new session and removing all references to objects from the current session. in : reset input history out : reset output history dhist : reset directory history array : reset only variables that are NumPy arrays See Also -------- reset_selective : invoked as ``%reset_selective`` Examples -------- :: In [6]: a = 1 In [7]: a Out[7]: 1 In [8]: 'a' in _ip.user_ns Out[8]: True
Python 3.6.4 (v3.6.4:d48eceb, Dec 192017, 06:54:40) [MSC v.190064 bit (AMD64)] Type 'copyright', 'credits'or'license'for more information IPython 6.2.1 -- An enhanced Interactive Python. Type '?'for help. Using matplotlib backend: TkAgg
In [20]: %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
In [23]: !python Python 3.6.4 (v3.6.4:d48eceb, Dec 192017, 06:54:40) [MSC v.190064 bit (AMD64)] on win32 Type "help", "copyright", "credits"or"license"for more information. >>>
还可以将shell命令的控制台输出存放到变量中,只需将 ! 开头的表达式赋值给变量:
1 2 3 4
In [152]: ip_info = !ls
In [153]: ip_info Out[153]: ['experiment.py', 'ipython_bug.py', 'ipython_script_test.py']
In [45]: %run ipython_bug.py --------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) D:Pythonipythonipython_bug.py in <module>() 5 b = 2 6 c = 0 ----> 7 result = f(a, b, c)
D:Pythonipythonipython_bug.py in f(x, y, z) 1deff(x, y, z): ----> 2 return (x+y) /z 3 4 a = 1 5 b = 2
ZeroDivisionError: division by zero
In [46]: %debug > d:pythonipythonipython_bug.py(2)f() 1deff(x, y, z): ----> 2 return (x+y) /z 3 4 a = 1 5 b = 2
In [50]: %run -d ipython_bug.py Breakpoint 1 at d:pythonipythonipython_bug.py:1 NOTE: Enter 'c' at the ipdb> prompt to continue execution. > d:pythonipythonipython_bug.py(1)<module>() 1---> 1 def f(x, y, z): 2return (x+y) /z 3 4 a = 1 5 b = 2
ipdb> s > d:pythonipythonipython_bug.py(4)<module>() 2return (x+y) /z 3 ----> 4 a = 1 5 b = 2 6 c = 0
ipdb> s > d:pythonipythonipython_bug.py(5)<module>() 3 4 a = 1 ----> 5 b = 2 6 c = 0 7 result = f(a, b, c)
ipdb> s > d:pythonipythonipython_bug.py(6)<module>() 3 4 a = 1 5 b = 2 ----> 6 c = 0 7 result = f(a, b, c)
In [53]: %run -d ipython_bug.py Breakpoint 1 at d:pythonipythonipython_bug.py:1 NOTE: Enter 'c' at the ipdb> prompt to continue execution. > d:pythonipythonipython_bug.py(1)<module>() 1---> 1 def f(x, y, z): 2return (x+y) /z 3 4 a = 1 5 b = 2
ipdb> b 7 Breakpoint 2 at d:pythonipythonipython_bug.py:7 ipdb> c > d:pythonipythonipython_bug.py(7)<module>() 3 4 a = 1 5 b = 2 6 c = 0 2---> 7 result = f(a, b, c)
ipdb> n ZeroDivisionError: division by zero > d:pythonipythonipython_bug.py(7)<module>() 3 4 a = 1 5 b = 2 6 c = 0 2---> 7 result = f(a, b, c)
ipdb> n --Return-- None > d:pythonipythonipython_bug.py(7)<module>() 3 4 a = 1 5 b = 2 6 c = 0 2---> 7 result = f(a, b, c)
IPython调试器命令
命令
功能
h(elp)
显示命令列表
help command
显示command的文档
c(ontinue)
恢复程序的执行
q(uit)
退出调试器,不再执行任何代码
b(readk) number
在当前文件的第number行设置一个断点
b path/to/file.py:number
在指定文件的第number行设置一个断点
s(tep)
单步进入函数调用
n(ext)
执行当前行,并前进到当前级别的下一行
u(p)/d(own)
在函数调用栈中向上或向下移动
a(rgs)
显示当前函数的参数
debug statement
在新的(递归)调试器中调用语句statement
l(ist) statement
显示当前行,以及当前栈级别的上下文参考代码
w(here)
打印当前位置的完整栈跟踪(包括上下文参考代码)
测试代码的执行时间:%time和%timeit
%time一次执行一条语句,然后报告总体执行时间
1 2 3 4 5 6 7
In [56]: strings = ['foo','bar','abc','foobar','python','Guide Peple']*100000
In [57]: %time method1 = [x for x in strings if x.startswith('foo')] Wall time: 102 ms
In [58]: %time method2 = [x for x in strings if x[:3] == 'foo'] Wall time: 59.2 ms
%timeit对于任意语句,它会自动多次执行以产生一个非常精确的平均执行时间
1 2 3 4 5
In [59]: %timeit method1 = [x for x in strings if x.startswith('foo')] 100 ms ± 5.73 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [60]: %timeit method2 = [x for x in strings if x[:3] == 'foo'] 57 ms ± 7.12 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [141]: %cpaste Pasting code; enter '--' alone on the line to stop or use Ctrl-D. :defrun_experiment(niter=100): k = 100 results = [] for _ in range(niter): mat = np.random.randn(k, k) max_eigenvalue = np.abs(eigvals(mat)).max() results.append(max_eigenvalue) return results::::::: : :--
In [142]: %prun -l 7 -s cumulative run_experiment() 3804 function calls in0.901 seconds
Ordered by: cumulative time List reduced from31 to 7 due to restriction <7>