forked from yasoob/intermediatePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugging.html
More file actions
94 lines (82 loc) · 4.45 KB
/
debugging.html
File metadata and controls
94 lines (82 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Debugging</title>
<link rel="stylesheet" href="_static/epub.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="generators.html" title="Generators"
accesskey="N">next</a></li>
<li class="right" >
<a href="args_and_kwargs.html" title="*args and **kwargs"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Python Tips 0.1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="body" role="main">
<div class="section" id="debugging">
<h1>Debugging</h1>
<p>Debuggin is also something which once mastered can greatly enhance your
bug hunting skills. Most of the newcommers neglect the importance of the
Python debugger (<code class="docutils literal"><span class="pre">pdb</span></code>). In this section I am going to tell you only a
few important commands. You can learn more about it from the official
documentation.</p>
<p><strong>Running from commandline</strong></p>
<p>You can run a script from the commandline using the Python debugger.
Here is an example:</p>
<div class="code python highlight-python"><div class="highlight"><pre>$ python -m pdb my_script.py
</pre></div>
</div>
<p>It would cause the debugger to stop the execution on the first statement
it finds. This is helpful if you script is short. You can then inspect
the variables and continue execution line-by-line.</p>
<p><strong>Running from inside a script</strong></p>
<p>You can set break points in the script itself so that you can inspect
the variables and stuff at particular points. This is possible using the
<code class="docutils literal"><span class="pre">pdb.set_trace()</span></code> method. Here is an example:</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pdb</span>
<span class="k">def</span> <span class="nf">make_bread</span><span class="p">():</span>
<span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span>
<span class="k">return</span> <span class="s">"I don't have time"</span>
<span class="k">print</span><span class="p">(</span><span class="n">make_bread</span><span class="p">())</span>
</pre></div>
</div>
<p>Try running the above script after saving it. You would enter the
debugger as soon as you run it. Now it’s time to learn some of the
commands of the debugger.</p>
<p><strong>Commands:</strong></p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">c</span></code>: continue execution</li>
<li><code class="docutils literal"><span class="pre">w</span></code>: shows the context of the current line it is executing.</li>
<li><code class="docutils literal"><span class="pre">a</span></code>: print the argument list of the current function</li>
<li><code class="docutils literal"><span class="pre">s</span></code>: Execute the current line and stop at the first possible
occasion.</li>
<li><code class="docutils literal"><span class="pre">n</span></code>: Continue execution until the next line in the current function
is reached or it returns.</li>
</ul>
<p>The difference between <code class="docutils literal"><span class="pre">n</span></code>ext and <code class="docutils literal"><span class="pre">s</span></code>tep is that step stops
inside a called function, while next executes called functions at
(nearly) full speed, only stopping at the next line in the current
function.</p>
<p>These are just a few commands. <code class="docutils literal"><span class="pre">pdb</span></code> also supports post mortem. It is
also a really handy function. I would highly suggest you to look at the
official documentation and learn more about it.</p>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer" role="contentinfo">
© Copyright 2015, Muhammad Yasoob Ullah Khalid.
</div>
</body>
</html>