{"id":1170205,"date":"2025-01-15T16:19:42","date_gmt":"2025-01-15T08:19:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1170205.html"},"modified":"2025-01-15T16:19:44","modified_gmt":"2025-01-15T08:19:44","slug":"python%e5%a6%82%e4%bd%95debug%e6%89%be%e5%87%ba%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1170205.html","title":{"rendered":"python\u5982\u4f55debug\u627e\u51fa\u9519\u8bef"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070938\/cbacf96a-8290-4cda-a1dc-5955a9f5f79c.webp\" alt=\"python\u5982\u4f55debug\u627e\u51fa\u9519\u8bef\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\u3001\u6253\u5370\u8c03\u8bd5\u4fe1\u606f\u3001\u4f7f\u7528\u65e5\u5fd7\u6a21\u5757\u3001<a href=\"https:\/\/docs.pingcode.com\/code-review\" target=\"_blank\">\u4ee3\u7801\u5ba1\u67e5<\/a><\/strong>\u662fPython\u4e2d\u5e38\u7528\u7684\u8c03\u8bd5\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5<\/strong>\u662f\u6700\u6709\u6548\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u8ba9\u4f60\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u6682\u505c\uff0c\u5e76\u68c0\u67e5\u53d8\u91cf\u7684\u503c\u548c\u7a0b\u5e8f\u7684\u72b6\u6001\uff0c\u4ece\u800c\u7cbe\u786e\u5b9a\u4f4d\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u65ad\u70b9\u8c03\u8bd5\u53ef\u4ee5\u901a\u8fc7Python\u81ea\u5e26\u7684\u8c03\u8bd5\u5de5\u5177<code>pdb<\/code>\u6216\u4e00\u4e9b\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u3001VS Code\u7b49\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>pdb<\/code>\u8fdb\u884c\u65ad\u70b9\u8c03\u8bd5\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165pdb\u6a21\u5757<\/strong>\uff1a\u5728\u4ee3\u7801\u4e2d\u9700\u8981\u8c03\u8bd5\u7684\u5730\u65b9\u5bfc\u5165<code>pdb<\/code>\u6a21\u5757\uff0c\u5e76\u8c03\u7528<code>pdb.set_trace()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4f1a\u5728\u7a0b\u5e8f\u8fd0\u884c\u5230\u8be5\u884c\u65f6\u6682\u505c\uff0c\u5e76\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f\u3002<\/li>\n<li><strong>\u8fd0\u884c\u4ee3\u7801<\/strong>\uff1a\u8fd0\u884c\u4f60\u7684Python\u811a\u672c\u3002\u7a0b\u5e8f\u4f1a\u5728\u9047\u5230<code>pdb.set_trace()<\/code>\u7684\u5730\u65b9\u6682\u505c\uff0c\u5e76\u8fdb\u5165\u4ea4\u4e92\u5f0f\u8c03\u8bd5\u754c\u9762\u3002<\/li>\n<li><strong>\u4f7f\u7528\u8c03\u8bd5\u547d\u4ee4<\/strong>\uff1a\u5728\u8c03\u8bd5\u754c\u9762\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u8c03\u8bd5\u547d\u4ee4\u6765\u68c0\u67e5\u53d8\u91cf\u7684\u503c\u3001\u5355\u6b65\u6267\u884c\u4ee3\u7801\u3001\u7ee7\u7eed\u8fd0\u884c\u7b49\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>n<\/code>\u547d\u4ee4\u6267\u884c\u4e0b\u4e00\u884c\u4ee3\u7801\uff0c\u4f7f\u7528<code>c<\/code>\u547d\u4ee4\u7ee7\u7eed\u8fd0\u884c\u76f4\u5230\u4e0b\u4e00\u4e2a\u65ad\u70b9\u3002<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>pdb<\/code>\u8fdb\u884c\u65ad\u70b9\u8c03\u8bd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    x = 5<\/p>\n<p>    y = &#39;10&#39;<\/p>\n<p>    pdb.set_trace()  # \u8bbe\u7f6e\u65ad\u70b9<\/p>\n<p>    result = add(x, y)<\/p>\n<p>    print(result)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5f53\u7a0b\u5e8f\u8fd0\u884c\u5230<code>pdb.set_trace()<\/code>\u65f6\u4f1a\u6682\u505c\uff0c\u4f60\u53ef\u4ee5\u5728\u8c03\u8bd5\u754c\u9762\u4e2d\u8f93\u5165\u547d\u4ee4\u6765\u68c0\u67e5\u53d8\u91cf<code>x<\/code>\u548c<code>y<\/code>\u7684\u503c\uff0c\u5e76\u53d1\u73b0<code>add<\/code>\u51fd\u6570\u7684\u8c03\u7528\u4f1a\u5bfc\u81f4\u9519\u8bef\uff0c\u56e0\u4e3a<code>x<\/code>\u662f\u6574\u6570\u800c<code>y<\/code>\u662f\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u627e\u51fa\u5e76\u4fee\u590d\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5728Python\u4e2d\u8fdb\u884c\u8c03\u8bd5\u7684\u5404\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u65ad\u70b9\u8c03\u8bd5<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528pdb\u6a21\u5757<\/h4>\n<\/p>\n<p><p>Python\u81ea\u5e26\u7684\u8c03\u8bd5\u5668<code>pdb<\/code>\uff08Python Debugger\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u4ee3\u7801\u8fd0\u884c\u65f6\u63d2\u5165\u65ad\u70b9\uff0c\u68c0\u67e5\u53d8\u91cf\u7684\u503c\uff0c\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u4ee5\u627e\u5230\u5e76\u4fee\u590d\u9519\u8bef\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u8bbe\u7f6e\u65ad\u70b9<\/strong>\uff1a\u5728\u9700\u8981\u8c03\u8bd5\u7684\u5730\u65b9\u63d2\u5165<code>pdb.set_trace()<\/code>\u3002\u5f53\u7a0b\u5e8f\u8fd0\u884c\u5230\u8fd9\u4e00\u884c\u65f6\uff0c\u4f1a\u6682\u505c\u5e76\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import pdb<\/p>\n<p>def faulty_function(a, b):<\/p>\n<p>    pdb.set_trace()  # \u8bbe\u7f6e\u65ad\u70b9<\/p>\n<p>    return a \/ b<\/p>\n<p>faulty_function(4, 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5f53\u7a0b\u5e8f\u6267\u884c\u5230<code>pdb.set_trace()<\/code>\u65f6\uff0c\u4f1a\u6682\u505c\u5e76\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8c03\u8bd5\u547d\u4ee4\u6765\u68c0\u67e5\u53d8\u91cf\u7684\u503c\uff0c\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u627e\u51fa\u9519\u8bef\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u8c03\u8bd5\u547d\u4ee4<\/strong>\uff1a\u5728\u8c03\u8bd5\u6a21\u5f0f\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u8c03\u8bd5\u547d\u4ee4\uff0c\u5982\uff1a\n<ul>\n<li><code>n<\/code>\uff08next\uff09\uff1a\u6267\u884c\u4e0b\u4e00\u884c\u4ee3\u7801\u3002<\/li>\n<li><code>c<\/code>\uff08continue\uff09\uff1a\u7ee7\u7eed\u8fd0\u884c\u7a0b\u5e8f\uff0c\u76f4\u5230\u9047\u5230\u4e0b\u4e00\u4e2a\u65ad\u70b9\u3002<\/li>\n<li><code>s<\/code>\uff08step\uff09\uff1a\u8fdb\u5165\u51fd\u6570\u5185\u90e8\u3002<\/li>\n<li><code>q<\/code>\uff08quit\uff09\uff1a\u9000\u51fa\u8c03\u8bd5\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><h4>2. \u4f7f\u7528IDE\u7684\u8c03\u8bd5\u529f\u80fd<\/h4>\n<\/p>\n<p><p>\u73b0\u4ee3\u7684\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u3001VS Code\u3001Eclipse\u7b49\uff0c\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u53ef\u4ee5\u901a\u8fc7\u56fe\u5f62\u754c\u9762\u8bbe\u7f6e\u65ad\u70b9\uff0c\u68c0\u67e5\u53d8\u91cf\uff0c\u9010\u6b65\u6267\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u8bbe\u7f6e\u65ad\u70b9<\/strong>\uff1a\u5728IDE\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u4ee3\u7801\u884c\u53f7\u65c1\u8fb9\u70b9\u51fb\uff0c\u8bbe\u7f6e\u65ad\u70b9\u3002<\/li>\n<li><strong>\u542f\u52a8\u8c03\u8bd5<\/strong>\uff1a\u70b9\u51fb\u8c03\u8bd5\u6309\u94ae\uff0c\u7a0b\u5e8f\u4f1a\u5728\u9047\u5230\u65ad\u70b9\u65f6\u6682\u505c\u3002<\/li>\n<li><strong>\u68c0\u67e5\u53d8\u91cf<\/strong>\uff1a\u5728\u8c03\u8bd5\u6a21\u5f0f\u4e0b\uff0c\u53ef\u4ee5\u67e5\u770b\u53d8\u91cf\u7684\u503c\uff0c\u68c0\u67e5\u8c03\u7528\u6808\uff0c\u5206\u6790\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u3002<\/li>\n<li><strong>\u9010\u6b65\u6267\u884c<\/strong>\uff1a\u4f7f\u7528\u5355\u6b65\u6267\u884c\u3001\u8fdb\u5165\u51fd\u6570\u3001\u7ee7\u7eed\u8fd0\u884c\u7b49\u529f\u80fd\uff0c\u9010\u6b65\u6392\u67e5\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u6253\u5370\u8c03\u8bd5\u4fe1\u606f<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528print\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u5c31\u662f\u5728\u4ee3\u7801\u4e2d\u63d2\u5165<code>print<\/code>\u8bed\u53e5\uff0c\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u548c\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7b80\u5355\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def faulty_function(a, b):<\/p>\n<p>    print(f&quot;Debug: a = {a}, b = {b}&quot;)<\/p>\n<p>    return a \/ b<\/p>\n<p>faulty_function(4, 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>print<\/code>\u8bed\u53e5\u8f93\u51fa<code>a<\/code>\u548c<code>b<\/code>\u7684\u503c\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u53d1\u73b0\u95ee\u9898\u7684\u6240\u5728\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u683c\u5f0f\u5316\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u6e05\u6670\u5730\u8f93\u51fa\u8c03\u8bd5\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\uff0c\u5c06\u53d8\u91cf\u503c\u548c\u63cf\u8ff0\u4fe1\u606f\u7ed3\u5408\u8d77\u6765\uff0c\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def faulty_function(a, b):<\/p>\n<p>    print(f&quot;Debug: a = {a}, b = {b}&quot;)<\/p>\n<p>    try:<\/p>\n<p>        result = a \/ b<\/p>\n<p>    except ZeroDivisionError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        result = None<\/p>\n<p>    return result<\/p>\n<p>faulty_function(4, 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u5e76\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\uff0c\u53ef\u4ee5\u66f4\u6e05\u695a\u5730\u4e86\u89e3\u7a0b\u5e8f\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u4e86\u4ec0\u4e48\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u65e5\u5fd7\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1. \u914d\u7f6e\u65e5\u5fd7\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u76f8\u6bd4\u4e8e<code>print<\/code>\u8bed\u53e5\uff0cPython\u7684<code>logging<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u65e5\u5fd7\u529f\u80fd\uff0c\u53ef\u4ee5\u5c06\u8c03\u8bd5\u4fe1\u606f\u8f93\u51fa\u5230\u6587\u4ef6\u3001\u63a7\u5236\u53f0\uff0c\u751a\u81f3\u8fdc\u7a0b\u670d\u52a1\u5668\uff0c\u5e76\u652f\u6301\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u6a21\u5757<\/strong><\/h2>\n<p>logging.basicConfig(level=logging.DEBUG, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>def faulty_function(a, b):<\/p>\n<p>    logging.debug(f&quot;a = {a}, b = {b}&quot;)<\/p>\n<p>    try:<\/p>\n<p>        result = a \/ b<\/p>\n<p>    except ZeroDivisionError as e:<\/p>\n<p>        logging.error(f&quot;Error: {e}&quot;)<\/p>\n<p>        result = None<\/p>\n<p>    return result<\/p>\n<p>faulty_function(4, 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u914d\u7f6e<code>logging<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5c06\u8c03\u8bd5\u4fe1\u606f\u8f93\u51fa\u5230\u63a7\u5236\u53f0\uff0c\u5e76\u6839\u636e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\uff08\u5982DEBUG\u3001INFO\u3001WARNING\u3001ERROR\u3001CRITICAL\uff09\u8fdb\u884c\u5206\u7c7b\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b<\/h4>\n<\/p>\n<p><p><code>logging<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65e5\u5fd7\u7ea7\u522b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u7ea7\u522b\u8bb0\u5f55\u8c03\u8bd5\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ul>\n<li><code>DEBUG<\/code>\uff1a\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u901a\u5e38\u53ea\u5728\u8c03\u8bd5\u65f6\u4f7f\u7528\u3002<\/li>\n<li><code>INFO<\/code>\uff1a\u786e\u8ba4\u7a0b\u5e8f\u6309\u9884\u671f\u8fd0\u884c\u7684\u4fe1\u606f\u3002<\/li>\n<li><code>WARNING<\/code>\uff1a\u8868\u793a\u53ef\u80fd\u51fa\u73b0\u95ee\u9898\u7684\u8b66\u544a\u4fe1\u606f\u3002<\/li>\n<li><code>ERROR<\/code>\uff1a\u7531\u4e8e\u4e25\u91cd\u95ee\u9898\u5bfc\u81f4\u7a0b\u5e8f\u672a\u80fd\u6267\u884c\u67d0\u4e9b\u529f\u80fd\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<li><code>CRITICAL<\/code>\uff1a\u4e25\u91cd\u9519\u8bef\uff0c\u8868\u793a\u7a0b\u5e8f\u53ef\u80fd\u65e0\u6cd5\u7ee7\u7eed\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u548c\u5206\u6790\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4ee3\u7801\u5ba1\u67e5<\/h3>\n<\/p>\n<p><h4>1. \u81ea\u6211\u5ba1\u67e5<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5b9a\u671f\u5bf9\u81ea\u5df1\u7684\u4ee3\u7801\u8fdb\u884c\u5ba1\u67e5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u53d1\u73b0\u6f5c\u5728\u7684\u95ee\u9898\u548c\u9519\u8bef\u3002\u901a\u8fc7\u9605\u8bfb\u548c\u7406\u89e3\u4ee3\u7801\uff0c\u68c0\u67e5\u903b\u8f91\u662f\u5426\u6b63\u786e\uff0c\u53d8\u91cf\u662f\u5426\u6b63\u786e\u4f7f\u7528\uff0c\u8f93\u5165\u8f93\u51fa\u662f\u5426\u5408\u7406\u7b49\uff0c\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u9519\u8bef\u7684\u53d1\u751f\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u68c0\u67e5\u903b\u8f91<\/strong>\uff1a\u786e\u4fdd\u4ee3\u7801\u903b\u8f91\u6b63\u786e\uff0c\u907f\u514d\u903b\u8f91\u9519\u8bef\u3002<\/li>\n<li><strong>\u68c0\u67e5\u53d8\u91cf<\/strong>\uff1a\u786e\u4fdd\u53d8\u91cf\u4f7f\u7528\u6b63\u786e\uff0c\u907f\u514d\u53d8\u91cf\u540d\u51b2\u7a81\u548c\u672a\u5b9a\u4e49\u53d8\u91cf\u3002<\/li>\n<li><strong>\u68c0\u67e5\u8f93\u5165\u8f93\u51fa<\/strong>\uff1a\u786e\u4fdd\u8f93\u5165\u8f93\u51fa\u5408\u7406\uff0c\u907f\u514d\u8f93\u5165\u8f93\u51fa\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><h4>2. \u4ee3\u7801\u8bc4\u5ba1<\/h4>\n<\/p>\n<p><p>\u4ee3\u7801\u8bc4\u5ba1\uff08Code Review\uff09\u662f\u6307\u7531\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u5bf9\u4ee3\u7801\u8fdb\u884c\u68c0\u67e5\u548c\u8bc4\u5ba1\uff0c\u4ee5\u53d1\u73b0\u548c\u4fee\u590d\u6f5c\u5728\u7684\u95ee\u9898\u548c\u9519\u8bef\u3002\u901a\u8fc7\u4ee3\u7801\u8bc4\u5ba1\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\uff0c\u51cf\u5c11\u9519\u8bef\u7684\u53d1\u751f\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u540c\u4e8b\u8bc4\u5ba1<\/strong>\uff1a\u9080\u8bf7\u56e2\u961f\u4e2d\u7684\u5176\u4ed6\u6210\u5458\u5bf9\u4ee3\u7801\u8fdb\u884c\u8bc4\u5ba1\uff0c\u63d0\u4f9b\u53cd\u9988\u548c\u5efa\u8bae\u3002<\/li>\n<li><strong>\u5de5\u5177\u8bc4\u5ba1<\/strong>\uff1a\u4f7f\u7528\u4ee3\u7801\u5ba1\u67e5\u5de5\u5177\uff08\u5982Gerrit\u3001Review Board\u7b49\uff09\uff0c\u81ea\u52a8\u68c0\u67e5\u4ee3\u7801\u4e2d\u7684\u6f5c\u5728\u95ee\u9898\u548c\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee3\u7801\u8bc4\u5ba1\uff0c\u53ef\u4ee5\u53ca\u65f6\u53d1\u73b0\u548c\u4fee\u590d\u9519\u8bef\uff0c\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5355\u5143\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><h4>1. \u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u5355\u5143\u6d4b\u8bd5\u662f\u6307\u5bf9\u7a0b\u5e8f\u4e2d\u7684\u5404\u4e2a\u529f\u80fd\u5355\u5143\uff08\u51fd\u6570\u3001\u65b9\u6cd5\u3001\u7c7b\u7b49\uff09\u8fdb\u884c\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u6309\u9884\u671f\u5de5\u4f5c\u3002\u901a\u8fc7\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u53ca\u65f6\u53d1\u73b0\u548c\u4fee\u590d\u9519\u8bef\uff0c\u786e\u4fdd\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>class TestAddFunction(unittest.TestCase):<\/p>\n<p>    def test_add_positive_numbers(self):<\/p>\n<p>        self.assertEqual(add(1, 2), 3)<\/p>\n<p>    def test_add_negative_numbers(self):<\/p>\n<p>        self.assertEqual(add(-1, -2), -3)<\/p>\n<p>    def test_add_zero(self):<\/p>\n<p>        self.assertEqual(add(0, 0), 0)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u9a8c\u8bc1<code>add<\/code>\u51fd\u6570\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u7684\u884c\u4e3a\u662f\u5426\u6b63\u786e\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6d4b\u8bd5\u6846\u67b6<\/h4>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6d4b\u8bd5\u6846\u67b6\uff0c\u5982<code>unittest<\/code>\u3001<code>pytest<\/code>\u3001<code>nose<\/code>\u7b49\uff0c\u53ef\u4ee5\u5e2e\u52a9\u7f16\u5199\u548c\u6267\u884c\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>unittest<\/strong>\uff1aPython\u81ea\u5e26\u7684\u6d4b\u8bd5\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u6d4b\u8bd5\u529f\u80fd\u3002<\/li>\n<li><strong>pytest<\/strong>\uff1a\u529f\u80fd\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u6d4b\u8bd5\u6846\u67b6\uff0c\u652f\u6301\u66f4\u4e30\u5bcc\u7684\u6d4b\u8bd5\u529f\u80fd\u548c\u63d2\u4ef6\u3002<\/li>\n<li><strong>nose<\/strong>\uff1a\u6269\u5c55\u4e86<code>unittest<\/code>\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u6d4b\u8bd5\u8bed\u6cd5\u548c\u529f\u80fd\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u6d4b\u8bd5\u6846\u67b6\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u7f16\u5199\u548c\u7ba1\u7406\u5355\u5143\u6d4b\u8bd5\uff0c\u63d0\u9ad8\u6d4b\u8bd5\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177<\/h3>\n<\/p>\n<p><h4>1. \u4ee3\u7801\u9759\u6001\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u53ef\u4ee5\u5728\u4e0d\u8fd0\u884c\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u5206\u6790\u4ee3\u7801\u4e2d\u7684\u6f5c\u5728\u95ee\u9898\u548c\u9519\u8bef\u3002\u901a\u8fc7\u9759\u6001\u4ee3\u7801\u5206\u6790\uff0c\u53ef\u4ee5\u53d1\u73b0\u8bed\u6cd5\u9519\u8bef\u3001\u53d8\u91cf\u672a\u5b9a\u4e49\u3001\u7c7b\u578b\u4e0d\u5339\u914d\u7b49\u95ee\u9898\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>pylint<\/strong>\uff1aPython\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u68c0\u67e5\u4ee3\u7801\u4e2d\u7684\u8bed\u6cd5\u9519\u8bef\u3001\u98ce\u683c\u95ee\u9898\u7b49\u3002<\/li>\n<li><strong>flake8<\/strong>\uff1a\u7ed3\u5408\u4e86<code>pyflakes<\/code>\u3001<code>pycodestyle<\/code>\u548c<code>mccabe<\/code>\u7684\u529f\u80fd\uff0c\u63d0\u4f9b\u4e86\u66f4\u5168\u9762\u7684\u4ee3\u7801\u68c0\u67e5\u3002<\/li>\n<li><strong>mypy<\/strong>\uff1aPython\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\uff0c\u53ef\u4ee5\u68c0\u67e5\u4ee3\u7801\u4e2d\u7684\u7c7b\u578b\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><h4>2. \u4f7f\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u53ca\u65f6\u53d1\u73b0\u548c\u4fee\u590d\u6f5c\u5728\u7684\u95ee\u9898\u548c\u9519\u8bef\uff0c\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u5b89\u88c5pylint<\/p>\n<p>pip install pylint<\/p>\n<h2><strong>\u8fd0\u884cpylint<\/strong><\/h2>\n<p>pylint your_script.py<\/p>\n<h2><strong>\u5b89\u88c5flake8<\/strong><\/h2>\n<p>pip install flake8<\/p>\n<h2><strong>\u8fd0\u884cflake8<\/strong><\/h2>\n<p>flake8 your_script.py<\/p>\n<h2><strong>\u5b89\u88c5mypy<\/strong><\/h2>\n<p>pip install mypy<\/p>\n<h2><strong>\u8fd0\u884cmypy<\/strong><\/h2>\n<p>mypy your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u547d\u4ee4\uff0c\u53ef\u4ee5\u4f7f\u7528\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\u68c0\u67e5\u4ee3\u7801\u4e2d\u7684\u6f5c\u5728\u95ee\u9898\uff0c\u5e76\u6839\u636e\u5de5\u5177\u7684\u53cd\u9988\u8fdb\u884c\u4fee\u590d\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u7248\u672c\u63a7\u5236<\/h3>\n<\/p>\n<p><h4>1. \u7248\u672c\u63a7\u5236\u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08\u5982Git\u3001SVN\u7b49\uff09\uff0c\u53ef\u4ee5\u8bb0\u5f55\u4ee3\u7801\u7684\u53d8\u66f4\u5386\u53f2\uff0c\u65b9\u4fbf\u56de\u6eaf\u548c\u6062\u590d\u3002\u901a\u8fc7\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff0c\u53ef\u4ee5\u5728\u53d1\u73b0\u9519\u8bef\u65f6\uff0c\u5feb\u901f\u627e\u5230\u9519\u8bef\u7684\u5f15\u5165\u70b9\uff0c\u5e76\u8fdb\u884c\u4fee\u590d\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>Git<\/strong>\uff1a\u5206\u5e03\u5f0f\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5f00\u6e90\u9879\u76ee\u548c\u4f01\u4e1a\u5f00\u53d1\u3002<\/li>\n<li><strong>SVN<\/strong>\uff1a\u96c6\u4e2d\u5f0f\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff0c\u9002\u7528\u4e8e\u56e2\u961f\u534f\u4f5c\u5f00\u53d1\u3002<\/li>\n<\/ul>\n<p><h4>2. \u4f7f\u7528Git\u8fdb\u884c\u8c03\u8bd5<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528Git\u8fdb\u884c\u8c03\u8bd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u67e5\u770b\u4ee3\u7801\u7684\u53d8\u66f4\u5386\u53f2\uff0c\u627e\u5230\u5f15\u5165\u9519\u8bef\u7684\u63d0\u4ea4\uff0c\u5e76\u8fdb\u884c\u4fee\u590d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u67e5\u770b\u63d0\u4ea4\u5386\u53f2<\/p>\n<p>git log<\/p>\n<h2><strong>\u67e5\u770b\u67d0\u4e2a\u63d0\u4ea4\u7684\u53d8\u66f4<\/strong><\/h2>\n<p>git show commit_id<\/p>\n<h2><strong>\u56de\u9000\u5230\u67d0\u4e2a\u63d0\u4ea4<\/strong><\/h2>\n<p>git checkout commit_id<\/p>\n<h2><strong>\u521b\u5efa\u8c03\u8bd5\u5206\u652f<\/strong><\/h2>\n<p>git checkout -b debug-branch<\/p>\n<h2><strong>\u5728\u8c03\u8bd5\u5206\u652f\u4e0a\u8fdb\u884c\u8c03\u8bd5<\/strong><\/h2>\n<h2><strong>...<\/strong><\/h2>\n<h2><strong>\u4fee\u590d\u9519\u8bef\u540e\u5408\u5e76\u5230\u4e3b\u5206\u652f<\/strong><\/h2>\n<p>git checkout main<\/p>\n<p>git merge debug-branch<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u547d\u4ee4\uff0c\u53ef\u4ee5\u4f7f\u7528Git\u8fdb\u884c\u8c03\u8bd5\uff0c\u627e\u5230\u5e76\u4fee\u590d\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><h4>1. \u6355\u83b7\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u901a\u8fc7\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u56e0\u9519\u8bef\u800c\u5d29\u6e83\uff0c\u5e76\u63d0\u4f9b\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u5e2e\u52a9\u8c03\u8bd5\u548c\u4fee\u590d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def faulty_function(a, b):<\/p>\n<p>    try:<\/p>\n<p>        result = a \/ b<\/p>\n<p>    except ZeroDivisionError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        result = None<\/p>\n<p>    return result<\/p>\n<p>faulty_function(4, 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u6355\u83b7<code>ZeroDivisionError<\/code>\u5f02\u5e38\uff0c\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u56e0\u9664\u96f6\u9519\u8bef\u800c\u5d29\u6e83\uff0c\u5e76\u8f93\u51fa\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>2. \u81ea\u5b9a\u4e49\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\uff0c\u4ee5\u63d0\u4f9b\u66f4\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u548c\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class CustomError(Exception):<\/p>\n<p>    pass<\/p>\n<p>def faulty_function(a, b):<\/p>\n<p>    if b == 0:<\/p>\n<p>        raise CustomError(&quot;Division by zero is not allowed&quot;)<\/p>\n<p>    return a \/ b<\/p>\n<p>try:<\/p>\n<p>    faulty_function(4, 0)<\/p>\n<p>except CustomError as e:<\/p>\n<p>    print(f&quot;Error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5b9a\u4e49\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b<code>CustomError<\/code>\uff0c\u53ef\u4ee5\u63d0\u4f9b\u66f4\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u548c\u5904\u7406\u903b\u8f91\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u7c7b\u578b\u6ce8\u89e3<\/h3>\n<\/p>\n<p><h4>1. \u6dfb\u52a0\u7c7b\u578b\u6ce8\u89e3<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u7c7b\u578b\u6ce8\u89e3\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5e76\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u53ca\u65f6\u53d1\u73b0\u7c7b\u578b\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a: int, b: int) -&gt; int:<\/p>\n<p>    return a + b<\/p>\n<p>result = add(1, 2)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u6dfb\u52a0\u7c7b\u578b\u6ce8\u89e3\uff0c\u53ef\u4ee5\u660e\u786e\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u907f\u514d\u7c7b\u578b\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u7c7b\u578b\u68c0\u67e5\u5de5\u5177<\/h4>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\uff0c\u5982<code>mypy<\/code>\u3001<code>pytype<\/code>\u7b49\uff0c\u53ef\u4ee5\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u68c0\u67e5\u4ee3\u7801\u4e2d\u7684\u7c7b\u578b\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u5b89\u88c5mypy<\/p>\n<p>pip install mypy<\/p>\n<h2><strong>\u8fd0\u884cmypy<\/strong><\/h2>\n<p>mypy your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u53ca\u65f6\u53d1\u73b0\u548c\u4fee\u590d\u7c7b\u578b\u9519\u8bef\uff0c\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u8c03\u8bd5\u5668<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528IPython\u8c03\u8bd5\u5668<\/h4>\n<\/p>\n<p><p>IPython\u662f\u4e00\u4e2a\u589e\u5f3a\u7248\u7684Python\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u8c03\u8bd5\u529f\u80fd\u3002\u901a\u8fc7\u4f7f\u7528IPython\u8c03\u8bd5\u5668\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u68c0\u67e5\u53d8\u91cf\u7684\u503c\uff0c\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u627e\u51fa\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from IPython.core.debugger import set_trace<\/p>\n<p>def faulty_function(a, b):<\/p>\n<p>    set_trace()  # \u8bbe\u7f6e\u65ad\u70b9<\/p>\n<p>    return a \/ b<\/p>\n<p>faulty_function(4, 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>set_trace()<\/code>\u8bbe\u7f6e\u65ad\u70b9\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u8fd0\u884c\u65f6\u6682\u505c\uff0c\u5e76\u8fdb\u5165IPython\u8c03\u8bd5\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5176\u4ed6\u8c03\u8bd5\u5668<\/h4>\n<\/p>\n<p><p>\u9664\u4e86IPython\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u8c03\u8bd5\u5668\uff0c\u5982<code>pudb<\/code>\u3001<code>ipdb<\/code>\u7b49\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u8c03\u8bd5\u5668\u8fdb\u884c\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u5b89\u88c5pudb<\/p>\n<p>pip install pudb<\/p>\n<h2><strong>\u8fd0\u884cpudb<\/strong><\/h2>\n<p>pudb your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u4e0d\u540c\u7684\u8c03\u8bd5\u5668\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u8fd0\u884c\u65f6\u68c0\u67e5\u53d8\u91cf\u7684\u503c\uff0c\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u627e\u51fa\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u4ee3\u7801\u8986\u76d6\u7387\u5de5\u5177<\/h3>\n<\/p>\n<p><h4>1. \u4ee3\u7801\u8986\u76d6\u7387<\/h4>\n<\/p>\n<p><p>\u4ee3\u7801\u8986\u76d6\u7387\u662f\u6307\u5728\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u7a0b\u5e8f\u4ee3\u7801\u88ab\u6267\u884c\u7684\u6bd4\u4f8b\u3002\u901a\u8fc7\u4f7f\u7528\u4ee3\u7801\u8986\u76d6\u7387\u5de5\u5177\uff0c\u53ef\u4ee5\u68c0\u67e5\u6d4b\u8bd5\u7528\u4f8b\u662f\u5426\u8986\u76d6\u4e86\u6240\u6709\u7684\u4ee3\u7801\u8def\u5f84\uff0c\u627e\u51fa\u672a\u6d4b\u8bd5\u7684\u4ee3\u7801\u90e8\u5206\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>coverage.py<\/strong>\uff1aPython\u4ee3\u7801\u8986\u76d6\u7387\u5de5\u5177\uff0c\u53ef\u4ee5\u68c0\u67e5\u6d4b\u8bd5\u7528\u4f8b\u7684\u8986\u76d6\u7387\u3002<\/li>\n<\/ul>\n<p><h4>2. \u4f7f\u7528coverage.py<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528coverage.py\uff0c\u53ef\u4ee5\u68c0\u67e5\u6d4b\u8bd5\u7528\u4f8b\u7684\u8986\u76d6\u7387\uff0c\u627e\u51fa\u672a\u6d4b\u8bd5\u7684\u4ee3\u7801\u90e8\u5206\uff0c\u5e76\u8865\u5145\u76f8\u5e94\u7684\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u5b89\u88c5coverage.py<\/p>\n<p>pip install coverage<\/p>\n<h2><strong>\u8fd0\u884c\u6d4b\u8bd5\u5e76\u751f\u6210\u8986\u76d6\u7387\u62a5\u544a<\/strong><\/h2>\n<p>coverage run -m unittest discover<\/p>\n<p>coverage report<\/p>\n<h2><strong>\u751f\u6210HTML\u683c\u5f0f\u7684\u8986\u76d6\u7387\u62a5\u544a<\/strong><\/h2>\n<p>coverage html<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u547d\u4ee4\uff0c\u53ef\u4ee5\u4f7f\u7528coverage.py\u68c0\u67e5\u6d4b\u8bd5\u7528\u4f8b\u7684\u8986\u76d6\u7387\uff0c\u5e76\u751f\u6210\u8986\u76d6\u7387\u62a5\u544a\uff0c\u627e\u51fa\u672a\u6d4b\u8bd5\u7684\u4ee3\u7801\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u6027\u80fd\u5206\u6790\u5de5\u5177<\/h3>\n<\/p>\n<p><h4>1. \u6027\u80fd\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u7684\u9519\u8bef\u53ef\u80fd\u662f\u7531\u4e8e\u6027\u80fd\u95ee\u9898\u5f15\u8d77\u7684\u3002\u901a\u8fc7\u4f7f\u7528\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u627e\u51fa\u7a0b\u5e8f\u4e2d\u6027\u80fd\u74f6\u9888\uff0c\u4f18\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>cProfile<\/strong>\uff1aPython\u5185\u7f6e\u7684\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u5206\u6790\u7a0b\u5e8f\u7684\u6027\u80fd\u74f6\u9888\u3002<\/li>\n<li><strong>line_profiler<\/strong>\uff1a\u9010\u884c\u5206\u6790\u4ee3\u7801\u6027\u80fd\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u627e\u51fa\u6027\u80fd\u74f6\u9888\u3002<\/li>\n<\/ul>\n<p><h4>2. \u4f7f\u7528cProfile<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528cProfile\uff0c\u53ef\u4ee5\u5206\u6790\u7a0b\u5e8f\u7684\u6027\u80fd\u74f6\u9888\uff0c\u627e\u51fa\u8017\u65f6\u8f83\u591a\u7684\u51fd\u6570\u548c\u4ee3\u7801\u6bb5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cProfile<\/p>\n<p>def slow_function():<\/p>\n<p>    for _ in range(1000000):<\/p>\n<p>        pass<\/p>\n<p>cProfile.run(&#39;slow_function()&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528cProfile\u5206\u6790<code>slow_function<\/code>\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u627e\u51fa\u7a0b\u5e8f\u7684\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528line_profiler<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528line_profiler\uff0c\u53ef\u4ee5\u9010\u884c\u5206\u6790\u4ee3\u7801\u7684\u6027\u80fd\uff0c\u627e\u51fa\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u5b89\u88c5line_profiler<\/p>\n<p>pip install line_profiler<\/p>\n<h2><strong>\u4f7f\u7528line_profiler\u5206\u6790\u4ee3\u7801\u6027\u80fd<\/strong><\/h2>\n<p>kernprof -l -v your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u547d\u4ee4<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u8c03\u8bd5\u4ee3\u7801\uff1f<\/strong><br \/>\u8c03\u8bd5Python\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u6700\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684\u8c03\u8bd5\u5de5\u5177\u5982<code>pdb<\/code>\u6a21\u5757\u3001\u5229\u7528IDE\u7684\u8c03\u8bd5\u529f\u80fd\u4ee5\u53ca\u6dfb\u52a0\u6253\u5370\u8bed\u53e5\u3002<code>pdb<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4ea4\u4e92\u5f0f\u8c03\u8bd5\u529f\u80fd\uff0c\u5141\u8bb8\u60a8\u9010\u884c\u6267\u884c\u4ee3\u7801\u5e76\u68c0\u67e5\u53d8\u91cf\u7684\u72b6\u6001\u3002\u6b64\u5916\uff0c\u8bb8\u591a\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u548cVSCode\u90fd\u6709\u56fe\u5f62\u5316\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u80fd\u591f\u66f4\u76f4\u89c2\u5730\u67e5\u770b\u8c03\u7528\u5806\u6808\u548c\u53d8\u91cf\u503c\u3002\u6709\u6548\u7684\u8c03\u8bd5\u6280\u5de7\u8fd8\u5305\u62ec\u5199\u5355\u5143\u6d4b\u8bd5\uff0c\u4ee5\u4fbf\u5728\u4ee3\u7801\u66f4\u6539\u540e\u5feb\u901f\u53d1\u73b0\u6f5c\u5728\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u7684\u5f02\u5e38\u5904\u7406\u6765\u6355\u83b7\u9519\u8bef\uff1f<\/strong><br \/>\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c\u4ece\u800c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002\u5728<code>try<\/code>\u5757\u4e2d\u653e\u7f6e\u53ef\u80fd\u5f15\u53d1\u9519\u8bef\u7684\u4ee3\u7801\uff0c\u5982\u679c\u53d1\u751f\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4f1a\u8df3\u8f6c\u5230<code>except<\/code>\u5757\uff0c\u5141\u8bb8\u60a8\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u6216\u91c7\u53d6\u5176\u4ed6\u7ea0\u6b63\u63aa\u65bd\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u7a0b\u5e8f\u7684\u6d41\u5411\uff0c\u5e76\u63d0\u4f9b\u7528\u6237\u53cb\u597d\u7684\u9519\u8bef\u6d88\u606f\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u7528\u7684Python\u8c03\u8bd5\u5de5\u5177\u548c\u5e93\uff1f<\/strong><br \/>\u9664\u4e86<code>pdb<\/code>\u6a21\u5757\u5916\uff0c\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u5de5\u5177\u548c\u5e93\u53ef\u4ee5\u5e2e\u52a9\u8c03\u8bd5Python\u4ee3\u7801\u3002\u4f8b\u5982\uff0c<code>ipdb<\/code>\u662f<code>pdb<\/code>\u7684\u589e\u5f3a\u7248\uff0c\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u7528\u6237\u754c\u9762\u548c\u529f\u80fd\u3002<code>PyCharm<\/code>\u548c<code>Visual Studio Code<\/code>\u7b49IDE\u90fd\u5185\u7f6e\u4e86\u5f3a\u5927\u7684\u8c03\u8bd5\u529f\u80fd\u3002\u5bf9\u4e8eWeb\u5f00\u53d1\u8005\uff0c<code>Flask<\/code>\u548c<code>Django<\/code>\u6846\u67b6\u63d0\u4f9b\u4e86\u8c03\u8bd5\u6a21\u5f0f\uff0c\u80fd\u591f\u5728\u8fd0\u884c\u65f6\u63d0\u4f9b\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u548c\u8c03\u7528\u5806\u6808\u3002\u6b64\u5916\uff0c<code>logging<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u6765\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\uff0c\u6709\u52a9\u4e8e\u5206\u6790\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\u3001\u6253\u5370\u8c03\u8bd5\u4fe1\u606f\u3001\u4f7f\u7528\u65e5\u5fd7\u6a21\u5757\u3001\u4ee3\u7801\u5ba1\u67e5\u662fPython\u4e2d\u5e38\u7528\u7684\u8c03\u8bd5\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\u662f\u6700\u6709\u6548\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1170214,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170205"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1170205"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170205\/revisions"}],"predecessor-version":[{"id":1170215,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170205\/revisions\/1170215"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1170214"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1170205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1170205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1170205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}