{"id":977945,"date":"2024-12-27T06:33:44","date_gmt":"2024-12-26T22:33:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/977945.html"},"modified":"2024-12-27T06:33:46","modified_gmt":"2024-12-26T22:33:46","slug":"python%e5%ad%97%e8%8a%82%e7%a0%81%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/977945.html","title":{"rendered":"python\u5b57\u8282\u7801\u5982\u4f55\u67e5\u770b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203144\/e0b0af0e-9c63-4982-8c6f-2af19697f6fa.webp\" alt=\"python\u5b57\u8282\u7801\u5982\u4f55\u67e5\u770b\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<br \/>\u8981\u67e5\u770bPython\u5b57\u8282\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u6a21\u5757<code>dis<\/code>\u3001\u4f7f\u7528<code>py_compile<\/code>\u6a21\u5757\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u6210\u5b57\u8282\u7801\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u5982<code>uncompyle6<\/code>\u8fdb\u884c\u53cd\u7f16\u8bd1\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u7406\u89e3Python\u4ee3\u7801\u7684\u5e95\u5c42\u6267\u884c\u8fc7\u7a0b\u3002<strong>\u4f7f\u7528\u5185\u7f6e\u6a21\u5757<code>dis<\/code>\u3001\u4f7f\u7528<code>py_compile<\/code>\u6a21\u5757\u7f16\u8bd1\u4ee3\u7801\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u53cd\u7f16\u8bd1<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u6a21\u5757<code>dis<\/code><\/strong>\u662f\u6700\u4e3a\u76f4\u63a5\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5bfc\u5165<code>dis<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728Python\u89e3\u91ca\u5668\u4e2d\u67e5\u770b\u51fd\u6570\u6216\u4ee3\u7801\u6bb5\u7684\u5b57\u8282\u7801\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6e05\u6670\u7684\u8868\u793a\uff0c\u5c55\u793a\u4e86Python\u4ee3\u7801\u5728\u89e3\u91ca\u5668\u4e2d\u662f\u5982\u4f55\u88ab\u6267\u884c\u7684\u3002\u901a\u8fc7\u5206\u6790\u8fd9\u4e9b\u5b57\u8282\u7801\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u4f18\u5316\u4ee3\u7801\u6027\u80fd\uff0c\u6216\u5e2e\u52a9\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u6a21\u5757<code>dis<\/code><\/p>\n<\/p>\n<p><p>Python\u7684<code>dis<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5c06Python\u4ee3\u7801\u53cd\u6c47\u7f16\u6210\u5b57\u8282\u7801\u3002\u5b57\u8282\u7801\u662fPython\u89e3\u91ca\u5668\u6267\u884c\u7684\u4f4e\u7ea7\u6307\u4ee4\u96c6\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5982\u4f55\u4f7f\u7528<code>dis<\/code>\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p>\u8981\u67e5\u770b\u5b57\u8282\u7801\uff0c\u9996\u5148\u8981\u5bfc\u5165<code>dis<\/code>\u6a21\u5757\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>dis.dis()<\/code>\u51fd\u6570\u6765\u53cd\u6c47\u7f16\u4e00\u4e2a\u51fd\u6570\u6216\u4ee3\u7801\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import dis<\/p>\n<p>def example_function(x):<\/p>\n<p>    return x + 1<\/p>\n<p>dis.dis(example_function)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u6253\u5370\u51fa<code>example_function<\/code>\u7684\u5b57\u8282\u7801\uff0c\u663e\u793a\u6bcf\u4e2a\u64cd\u4f5c\u7801\u53ca\u5176\u53c2\u6570\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7406\u89e3\u5b57\u8282\u7801\u8f93\u51fa<\/strong><\/li>\n<\/ol>\n<p><p>\u5b57\u8282\u7801\u8f93\u51fa\u901a\u5e38\u5305\u62ec\u64cd\u4f5c\u7801\u53ca\u5176\u53c2\u6570\u3002\u6bcf\u4e2a\u64cd\u4f5c\u7801\u4ee3\u8868\u4e00\u4e2a\u4f4e\u7ea7\u6307\u4ee4\uff0c\u5982\u52a0\u8f7d\u5e38\u91cf\u3001\u5b58\u50a8\u53d8\u91cf\u6216\u8c03\u7528\u51fd\u6570\u3002\u901a\u8fc7\u5206\u6790\u8fd9\u4e9b\u5b57\u8282\u7801\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6df1\u5165\u7406\u89e3Python\u4ee3\u7801\u662f\u5982\u4f55\u6267\u884c\u7684\uff0c\u6709\u52a9\u4e8e\u8c03\u8bd5\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>py_compile<\/code>\u6a21\u5757\u7f16\u8bd1\u4ee3\u7801<\/p>\n<\/p>\n<p><p><code>py_compile<\/code>\u6a21\u5757\u5141\u8bb8\u5f00\u53d1\u8005\u5c06Python\u6e90\u4ee3\u7801\u7f16\u8bd1\u6210\u5b57\u8282\u7801\u6587\u4ef6\uff08<code>.pyc<\/code>\u6587\u4ef6\uff09\uff0c\u8fd9\u4e9b\u6587\u4ef6\u901a\u5e38\u5728Python\u7a0b\u5e8f\u9996\u6b21\u8fd0\u884c\u65f6\u81ea\u52a8\u751f\u6210\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u7f16\u8bd1Python\u811a\u672c<\/strong><\/li>\n<\/ol>\n<p><p>\u8981\u624b\u52a8\u7f16\u8bd1Python\u811a\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>py_compile<\/code>\u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import py_compile<\/p>\n<p>py_compile.compile(&#39;example.py&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e2d\u751f\u6210\u4e00\u4e2a<code>__pycache__<\/code>\u76ee\u5f55\uff0c\u5176\u4e2d\u5305\u542b\u7f16\u8bd1\u540e\u7684\u5b57\u8282\u7801\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u67e5\u770b\u5b57\u8282\u7801\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u7f16\u8bd1\u540e\u7684<code>.pyc<\/code>\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7<code>dis<\/code>\u6a21\u5757\u52a0\u8f7d\u5e76\u67e5\u770b\u5176\u5b57\u8282\u7801\u3002\u4f7f\u7528<code>marshal<\/code>\u6a21\u5757\u53ef\u4ee5\u52a0\u8f7d\u5b57\u8282\u7801\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528<code>dis<\/code>\u6a21\u5757\u8fdb\u884c\u53cd\u6c47\u7f16\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u53cd\u7f16\u8bd1<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5185\u7f6e\u5de5\u5177\uff0c\u7b2c\u4e09\u65b9\u5de5\u5177\u5982<code>uncompyle6<\/code>\u53ef\u4ee5\u5c06\u5b57\u8282\u7801\u53cd\u7f16\u8bd1\u56dePython\u6e90\u4ee3\u7801\u3002\u8fd9\u5bf9\u4e8e\u7406\u89e3\u548c\u5206\u6790\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u4f7f\u7528<code>uncompyle6<\/code><\/strong><\/li>\n<\/ol>\n<p><p><code>uncompyle6<\/code>\u53ef\u4ee5\u901a\u8fc7<code>pip<\/code>\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install uncompyle6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u5c06<code>.pyc<\/code>\u6587\u4ef6\u53cd\u7f16\u8bd1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">uncompyle6 -o . example.pyc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5206\u6790\u53cd\u7f16\u8bd1\u7ed3\u679c<\/strong><\/li>\n<\/ol>\n<p><p>\u53cd\u7f16\u8bd1\u7684\u7ed3\u679c\u901a\u5e38\u63a5\u8fd1\u539f\u59cbPython\u4ee3\u7801\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u7406\u89e3\u5b57\u8282\u7801\u7684\u903b\u8f91\u3002\u8fd9\u5bf9\u4e8e\u6062\u590d\u4e22\u5931\u7684\u6e90\u4ee3\u7801\u6216\u5206\u6790\u7b2c\u4e09\u65b9\u5e93\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b57\u8282\u7801\u4f18\u5316\u548c\u8c03\u8bd5<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u67e5\u770b\u548c\u5206\u6790\u5b57\u8282\u7801\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8bc6\u522b\u4ee3\u7801\u4e2d\u7684\u6027\u80fd\u74f6\u9888\uff0c\u5e76\u8fdb\u884c\u4f18\u5316\u3002\u6b64\u5916\uff0c\u5b57\u8282\u7801\u5206\u6790\u5bf9\u8c03\u8bd5\u4e5f\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f18\u5316\u4ee3\u7801\u6027\u80fd<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7406\u89e3\u5b57\u8282\u7801\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8bc6\u522b\u5197\u4f59\u64cd\u4f5c\u6216\u4f4e\u6548\u7684\u4ee3\u7801\u6bb5\uff0c\u5e76\u8fdb\u884c\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u51fd\u6570\u8c03\u7528\u6216\u7b80\u5316\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8c03\u8bd5\u590d\u6742\u95ee\u9898<\/strong><\/li>\n<\/ol>\n<p><p>\u5b57\u8282\u7801\u5206\u6790\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u96be\u4ee5\u53d1\u73b0\u7684\u9519\u8bef\uff0c\u7279\u522b\u662f\u90a3\u4e9b\u4e0ePython\u89e3\u91ca\u5668\u7684\u6267\u884c\u6a21\u578b\u76f8\u5173\u7684\u95ee\u9898\u3002\u901a\u8fc7\u67e5\u770b\u5b57\u8282\u7801\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u6e05\u695a\u5730\u4e86\u89e3\u4ee3\u7801\u7684\u5b9e\u9645\u6267\u884c\u8fc7\u7a0b\uff0c\u5b9a\u4f4d\u95ee\u9898\u6839\u6e90\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6df1\u5165\u7406\u89e3Python\u5b57\u8282\u7801<\/p>\n<\/p>\n<p><p>Python\u5b57\u8282\u7801\u662f\u4e00\u4e2a\u4e2d\u95f4\u5c42\uff0c\u4ecb\u4e8e\u6e90\u4ee3\u7801\u548c\u673a\u5668\u7801\u4e4b\u95f4\u3002\u6df1\u5165\u7406\u89e3\u5b57\u8282\u7801\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u638c\u63e1Python\u7684\u6267\u884c\u6a21\u578b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b57\u8282\u7801\u7684\u7ed3\u6784<\/strong><\/li>\n<\/ol>\n<p><p>Python\u5b57\u8282\u7801\u7531\u4e00\u7cfb\u5217\u64cd\u4f5c\u7801\u7ec4\u6210\uff0c\u6bcf\u4e2a\u64cd\u4f5c\u7801\u901a\u5e38\u5bf9\u5e94\u4e00\u4e2a\u7279\u5b9a\u7684Python\u64cd\u4f5c\u3002\u7406\u89e3\u8fd9\u4e9b\u64cd\u4f5c\u7801\u7684\u529f\u80fd\u662f\u5206\u6790\u5b57\u8282\u7801\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5b57\u8282\u7801\u5728\u89e3\u91ca\u5668\u4e2d\u7684\u6267\u884c<\/strong><\/li>\n<\/ol>\n<p><p>\u5b57\u8282\u7801\u5728Python\u865a\u62df\u673a\u4e2d\u9010\u4e00\u6267\u884c\u3002Python\u89e3\u91ca\u5668\u901a\u8fc7\u4e00\u4e2a\u5faa\u73af\u6765\u8bfb\u53d6\u548c\u6267\u884c\u5b57\u8282\u7801\u6307\u4ee4\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u7c7b\u4f3c\u4e8e\u4e00\u4e2a\u7b80\u5355\u7684CPU\u6267\u884c\u673a\u5668\u6307\u4ee4\u3002\u901a\u8fc7\u5206\u6790\u5b57\u8282\u7801\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u4e86\u89e3Python\u89e3\u91ca\u5668\u5982\u4f55\u6267\u884c\u4ee3\u7801\uff0c\u63d0\u9ad8\u5bf9Python\u5185\u90e8\u673a\u5236\u7684\u7406\u89e3\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u67e5\u770b\u5b57\u8282\u7801\uff1f<\/strong><br \/>\u8981\u67e5\u770bPython\u4ee3\u7801\u7684\u5b57\u8282\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>dis<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>dis.dis()<\/code>\u51fd\u6570\uff0c\u4f20\u5165\u9700\u8981\u67e5\u770b\u7684\u51fd\u6570\u6216\u7c7b\u3002\u8fd9\u5c06\u8f93\u51fa\u76f8\u5e94\u7684\u5b57\u8282\u7801\u6307\u4ee4\uff0c\u5e2e\u52a9\u4f60\u7406\u89e3Python\u662f\u5982\u4f55\u6267\u884c\u4f60\u7684\u4ee3\u7801\u7684\u3002<\/p>\n<p><strong>\u5b57\u8282\u7801\u4e0e\u6e90\u4ee3\u7801\u4e4b\u95f4\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>\u5b57\u8282\u7801\u662fPython\u6e90\u4ee3\u7801\u7f16\u8bd1\u540e\u751f\u6210\u7684\u4e00\u79cd\u4e2d\u95f4\u8868\u793a\u5f62\u5f0f\uff0c\u65e8\u5728\u63d0\u9ad8\u4ee3\u7801\u6267\u884c\u6548\u7387\u3002\u6e90\u4ee3\u7801\u662f\u4eba\u7c7b\u53ef\u8bfb\u7684\u4ee3\u7801\uff0c\u5305\u542b\u4e86\u7a0b\u5e8f\u7684\u903b\u8f91\u548c\u7ed3\u6784\uff0c\u800c\u5b57\u8282\u7801\u5219\u662f\u673a\u5668\u53ef\u8bfb\u7684\u6307\u4ee4\uff0c\u901a\u5e38\u4e0d\u6613\u88ab\u4eba\u7406\u89e3\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u8981\u67e5\u770bPython\u7684\u5b57\u8282\u7801\uff1f<\/strong><br \/>\u67e5\u770b\u5b57\u8282\u7801\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u6df1\u5165\u7406\u89e3\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u4f18\u5316\u6027\u80fd\uff0c\u8c03\u8bd5\u590d\u6742\u95ee\u9898\uff0c\u6216\u5b66\u4e60Python\u7684\u5185\u90e8\u673a\u5236\u3002\u901a\u8fc7\u5206\u6790\u5b57\u8282\u7801\uff0c\u5f00\u53d1\u8005\u80fd\u591f\u8bc6\u522b\u6f5c\u5728\u7684\u6027\u80fd\u74f6\u9888\u6216\u4e0d\u5fc5\u8981\u7684\u5f00\u9500\uff0c\u4ece\u800c\u8fdb\u884c\u66f4\u9ad8\u6548\u7684\u7f16\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a\u8981\u67e5\u770bPython\u5b57\u8282\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u6a21\u5757dis\u3001\u4f7f\u7528py_compile\u6a21\u5757\u5c06Python\u4ee3\u7801\u7f16\u8bd1 [&hellip;]","protected":false},"author":3,"featured_media":977954,"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\/977945"}],"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=977945"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977945\/revisions"}],"predecessor-version":[{"id":977959,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977945\/revisions\/977959"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/977954"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=977945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=977945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=977945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}