{"id":1002989,"date":"2024-12-27T10:13:30","date_gmt":"2024-12-27T02:13:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1002989.html"},"modified":"2024-12-27T10:13:32","modified_gmt":"2024-12-27T02:13:32","slug":"python-%e4%b8%ad%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e8%a1%8c%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1002989.html","title":{"rendered":"python \u4e2d\u5982\u4f55\u663e\u793a\u884c\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080530\/8a3f9c5f-e770-40af-aa0e-7406f5568dbb.webp\" alt=\"python \u4e2d\u5982\u4f55\u663e\u793a\u884c\u53f7\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u663e\u793a\u884c\u53f7\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u7684\u529f\u80fd\u3001\u901a\u8fc7\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u8bbe\u7f6e\u3001\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u811a\u672c\u65f6\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\u3001\u4fee\u6539\u4ee3\u7801\u4ee5\u8f93\u51fa\u884c\u53f7\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u901a\u8fc7\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u8bbe\u7f6e<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u76f4\u89c2\u7684\u754c\u9762\u548c\u4fbf\u6377\u7684\u8c03\u8bd5\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u663e\u793a\u884c\u53f7\u7684\u9700\u6c42\u901a\u5e38\u51fa\u73b0\u5728\u8c03\u8bd5\u4ee3\u7801\u3001\u5206\u6790\u9519\u8bef\u65e5\u5fd7\u6216\u8005\u8fdb\u884c<a href=\"https:\/\/docs.pingcode.com\/code-review\" target=\"_blank\">\u4ee3\u7801\u5ba1\u67e5<\/a>\u65f6\u3002\u884c\u53f7\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u4e0b\u9762\u5c06\u5c55\u5f00\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u663e\u793a\u884c\u53f7\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u7684\u884c\u53f7\u663e\u793a<\/p>\n<\/p>\n<p><p>\u8bb8\u591a\u6587\u672c\u7f16\u8f91\u5668\uff0c\u5982Sublime Text\u3001Atom\u548cVisual Studio Code\uff0c\u63d0\u4f9b\u4e86\u663e\u793a\u884c\u53f7\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Sublime Text<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00Sublime Text\uff0c\u70b9\u51fb\u83dc\u5355\u680f\u7684\u201cView\u201d\uff0c\u7136\u540e\u9009\u62e9\u201cShow Line Numbers\u201d\u3002<\/li>\n<li>\u884c\u53f7\u4f1a\u81ea\u52a8\u663e\u793a\u5728\u7f16\u8f91\u5668\u5de6\u4fa7\uff0c\u5e2e\u52a9\u60a8\u5feb\u901f\u5b9a\u4f4d\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Atom<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u5728Atom\u4e2d\uff0c\u884c\u53f7\u901a\u5e38\u9ed8\u8ba4\u5f00\u542f\u3002\u5982\u679c\u6ca1\u6709\u663e\u793a\uff0c\u53ef\u4ee5\u70b9\u51fb\u201cView\u201d\u83dc\u5355\uff0c\u7136\u540e\u9009\u62e9\u201cToggle Line Numbers\u201d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Visual Studio Code<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u5728Visual Studio Code\u4e2d\uff0c\u884c\u53f7\u9ed8\u8ba4\u662f\u5f00\u542f\u7684\u3002\u5982\u679c\u5173\u95ed\u4e86\uff0c\u53ef\u4ee5\u901a\u8fc7\u70b9\u51fb\u5de6\u4e0b\u89d2\u7684\u9f7f\u8f6e\u56fe\u6807\u6253\u5f00\u201cSettings\u201d\uff0c\u5728\u641c\u7d22\u680f\u4e2d\u8f93\u5165\u201cline numbers\u201d\uff0c\u7136\u540e\u786e\u4fdd\u201cEditor: Line Numbers\u201d\u9009\u9879\u8bbe\u7f6e\u4e3a\u201con\u201d\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u8bbe\u7f6e<\/p>\n<\/p>\n<p><p>\u4f7f\u7528IDE\u8fdb\u884cPython\u5f00\u53d1\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u7ba1\u7406\u548c\u8c03\u8bd5\u9879\u76ee\uff0c\u8bb8\u591aIDE\u652f\u6301\u663e\u793a\u884c\u53f7\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>PyCharm<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00PyCharm\uff0c\u70b9\u51fb\u201cFile\u201d\u83dc\u5355\uff0c\u9009\u62e9\u201cSettings\u201d\u3002<\/li>\n<li>\u5728\u201cEditor\u201d\u4e0b\u627e\u5230\u201cGeneral\u201d\uff0c\u9009\u62e9\u201cAppearance\u201d\uff0c\u52fe\u9009\u201cShow line numbers\u201d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Eclipse with PyDev<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00Eclipse\uff0c\u9009\u62e9\u201cWindow\u201d\u83dc\u5355\uff0c\u7136\u540e\u9009\u62e9\u201cPreferences\u201d\u3002<\/li>\n<li>\u5728\u201cGeneral\u201d\u4e0b\u9009\u62e9\u201cEditors\u201d\uff0c\u7136\u540e\u9009\u62e9\u201cText Editors\u201d\uff0c\u52fe\u9009\u201cShow line numbers\u201d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Jupyter Notebook<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>Jupyter Notebook\u9ed8\u8ba4\u4e0d\u663e\u793a\u884c\u53f7\uff0c\u4f46\u53ef\u4ee5\u5728\u4ee3\u7801\u5355\u5143\u7684\u5de6\u8fb9\u680f\u4e2d\u70b9\u51fb\u884c\u53f7\u6309\u94ae\u6765\u663e\u793a\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u811a\u672c\u65f6\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cPython\u811a\u672c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u81ea\u5e26\u7684\u8c03\u8bd5\u5668\uff08pdb\uff09\u6765\u663e\u793a\u884c\u53f7\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528pdb\u6a21\u5757<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u5728\u811a\u672c\u4e2d\u5bfc\u5165pdb\u6a21\u5757\uff0c\u5e76\u5728\u9700\u8981\u8c03\u8bd5\u7684\u5730\u65b9\u63d2\u5165<code>pdb.set_trace()<\/code>\u3002<\/li>\n<li>\u8fd0\u884c\u811a\u672c\u540e\uff0c\u7a0b\u5e8f\u4f1a\u5728\u6307\u5b9a\u4f4d\u7f6e\u6682\u505c\uff0c\u5141\u8bb8\u60a8\u67e5\u770b\u5f53\u524d\u884c\u53f7\u4ee5\u53ca\u5176\u4ed6\u8c03\u8bd5\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u547d\u4ee4\u884c\u9009\u9879<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528<code>-m pdb script.py<\/code>\u6765\u8fd0\u884cPython\u811a\u672c\uff0c\u8fd9\u6837\u4f1a\u81ea\u52a8\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f\u5e76\u663e\u793a\u884c\u53f7\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4fee\u6539\u4ee3\u7801\u4ee5\u8f93\u51fa\u884c\u53f7<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u5728\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\u4e2d\u8f93\u51fa\u5f53\u524d\u884c\u53f7\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5185\u7f6e\u6a21\u5757\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528inspect\u6a21\u5757<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><code>inspect<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bbf\u95ee\u6d3b\u52a8\u5806\u6808\u5e27\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u5f53\u524d\u884c\u53f7\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>print(&quot;Current line number:&quot;, inspect.currentframe().f_lineno)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u65e5\u5fd7\u8f93\u51fa<\/strong>  <\/p>\n<\/p>\n<ul>\n<li>\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u65e5\u5fd7\u51fd\u6570\uff0c\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u6765\u5728\u65e5\u5fd7\u4e2d\u5305\u542b\u884c\u53f7\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>def log(message):<\/p>\n<p>    print(f&quot;{message} (Line: {inspect.currentframe().f_back.f_lineno})&quot;)<\/p>\n<p>log(&quot;This is a log message.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u8981\u9009\u62e9\u9002\u5408\u7684\u65b9\u5f0f\u5728Python\u4e2d\u663e\u793a\u884c\u53f7\uff0c\u4ece\u800c\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u4ee3\u7801\u5f00\u53d1\u548c\u8c03\u8bd5\u3002\u884c\u53f7\u7684\u663e\u793a\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\uff0c\u63d0\u5347\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5728\u6253\u5370\u8f93\u51fa\u65f6\u663e\u793a\u884c\u53f7\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u6267\u884c\u4ee3\u7801\u7684\u884c\u53f7\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u5728\u51fd\u6570\u4e2d\u4f7f\u7528<code>inspect.currentframe().f_lineno<\/code>\u83b7\u53d6\u5f53\u524d\u884c\u53f7\uff0c\u5e76\u5c06\u5176\u4e0e\u8f93\u51fa\u5185\u5bb9\u4e00\u8d77\u6253\u5370\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import inspect\n\ndef print_with_line_number(message):\n    line_number = inspect.currentframe().f_lineno\n    print(f&quot;Line {line_number}: {message}&quot;)\n\nprint_with_line_number(&quot;Hello, World!&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u811a\u672c\u4e2d\u8ddf\u8e2a\u9519\u8bef\u5e76\u663e\u793a\u76f8\u5173\u884c\u53f7\uff1f<\/strong><br \/>\u4f7f\u7528<code>traceback<\/code>\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728\u53d1\u751f\u5f02\u5e38\u65f6\u8ddf\u8e2a\u9519\u8bef\uff0c\u5e76\u663e\u793a\u9519\u8bef\u53d1\u751f\u7684\u884c\u53f7\u548c\u4ee3\u7801\u3002\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u5e76\u8c03\u7528<code>traceback.print_exc()<\/code>\uff0c\u53ef\u4ee5\u8f93\u51fa\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u5305\u62ec\u884c\u53f7\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import traceback\n\ntry:\n    # \u8fd9\u91cc\u6545\u610f\u5f15\u53d1\u4e00\u4e2a\u9519\u8bef\n    x = 1 \/ 0\nexcept Exception:\n    traceback.print_exc()\n<\/code><\/pre>\n<p><strong>\u5728IDE\u4e2d\u5982\u4f55\u81ea\u52a8\u663e\u793a\u884c\u53f7\uff1f<\/strong><br \/>\u5927\u591a\u6570\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u3001Visual Studio Code\u7b49\uff0c\u9ed8\u8ba4\u4f1a\u5728\u7f16\u8f91\u5668\u7684\u5de6\u4fa7\u663e\u793a\u884c\u53f7\u3002\u60a8\u53ef\u4ee5\u5728IDE\u7684\u8bbe\u7f6e\u4e2d\u786e\u8ba4\u8fd9\u4e00\u9009\u9879\u662f\u5426\u88ab\u542f\u7528\uff0c\u901a\u5e38\u5728\u201c\u8bbe\u7f6e\u201d\u6216\u201c\u504f\u597d\u8bbe\u7f6e\u201d\u4e2d\u627e\u5230\u201c\u7f16\u8f91\u5668\u201d\u6216\u201c\u4ee3\u7801\u7f16\u8f91\u201d\u76f8\u5173\u7684\u9009\u9879\u5373\u53ef\u3002\u8fd9\u6837\u5728\u7f16\u5199\u548c\u8c03\u8bd5\u4ee3\u7801\u65f6\uff0c\u884c\u53f7\u5c06\u59cb\u7ec8\u53ef\u89c1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u663e\u793a\u884c\u53f7\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u7684\u529f\u80fd\u3001\u901a\u8fc7\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u8bbe\u7f6e\u3001\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u811a\u672c [&hellip;]","protected":false},"author":3,"featured_media":1002997,"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\/1002989"}],"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=1002989"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1002989\/revisions"}],"predecessor-version":[{"id":1003002,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1002989\/revisions\/1003002"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1002997"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1002989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1002989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1002989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}