{"id":922115,"date":"2024-12-26T14:25:56","date_gmt":"2024-12-26T06:25:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/922115.html"},"modified":"2024-12-26T14:25:58","modified_gmt":"2024-12-26T06:25:58","slug":"python%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90pyc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/922115.html","title":{"rendered":"python\u5982\u4f55\u751f\u6210pyc"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210745\/bc901e2f-24b7-4f01-8b17-060cb1f94864.webp\" alt=\"python\u5982\u4f55\u751f\u6210pyc\" \/><\/p>\n<p><p> <strong>Python\u751f\u6210pyc\u6587\u4ef6\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u81ea\u52a8\u751f\u6210\u3001\u4f7f\u7528<code>compileall<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>py_compile<\/code>\u6a21\u5757<\/strong>\u3002\u5176\u4e2d\uff0c\u81ea\u52a8\u751f\u6210\u662f\u6307Python\u5728\u6267\u884c\u811a\u672c\u65f6\u81ea\u52a8\u521b\u5efapyc\u6587\u4ef6\uff1b<code>compileall<\/code>\u6a21\u5757\u5219\u7528\u4e8e\u6279\u91cf\u7f16\u8bd1\u76ee\u5f55\u4e0b\u7684\u6240\u6709Python\u6587\u4ef6\uff1b<code>py_compile<\/code>\u6a21\u5757\u7528\u4e8e\u7f16\u8bd1\u5355\u4e2aPython\u6587\u4ef6\u3002<strong>\u81ea\u52a8\u751f\u6210<\/strong>\u662fPython\u5728\u5bfc\u5165\u6a21\u5757\u65f6\u9ed8\u8ba4\u7684\u884c\u4e3a\uff0c<strong><code>compileall<\/code>\u6a21\u5757<\/strong>\u9002\u5408\u7528\u4e8e\u9700\u8981\u7f16\u8bd1\u6574\u4e2a\u9879\u76ee\u65f6\uff0c<strong><code>py_compile<\/code>\u6a21\u5757<\/strong>\u5219\u7528\u4e8e\u9700\u8981\u624b\u52a8\u63a7\u5236\u7f16\u8bd1\u8fc7\u7a0b\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u81ea\u52a8\u751f\u6210PYC\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5f53Python\u811a\u672c\u88ab\u6267\u884c\u65f6\uff0cPython\u4f1a\u81ea\u52a8\u5c06\u5176\u7f16\u8bd1\u6210\u5b57\u8282\u7801\u5e76\u751f\u6210<code>.pyc<\/code>\u6587\u4ef6\u5b58\u50a8\u5728<code>__pycache__<\/code>\u76ee\u5f55\u4e2d\u3002\u8fd9\u79cd\u65b9\u5f0f\u662f\u6700\u7b80\u5355\u548c\u5e38\u7528\u7684\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u81ea\u52a8\u751f\u6210\u673a\u5236<\/strong><br \/>\u5f53\u4f60\u8fd0\u884cPython\u7a0b\u5e8f\u65f6\uff0c\u89e3\u91ca\u5668\u4f1a\u81ea\u52a8\u7f16\u8bd1\u6240\u6709\u5bfc\u5165\u7684\u6a21\u5757\uff0c\u751f\u6210\u5bf9\u5e94\u7684<code>.pyc<\/code>\u6587\u4ef6\uff0c\u5b58\u50a8\u5728<code>__pycache__<\/code>\u76ee\u5f55\u4e2d\u3002\u8fd9\u662f\u4e3a\u4e86\u52a0\u901f\u4e0b\u6b21\u5bfc\u5165\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6267\u884c\u6d41\u7a0b<\/strong><br \/>\u8fd0\u884c\u811a\u672c\u65f6\uff1a  <\/p>\n<\/p>\n<ul>\n<li>Python\u68c0\u67e5\u6a21\u5757\u662f\u5426\u6709\u5bf9\u5e94\u7684<code>.pyc<\/code>\u6587\u4ef6\u3002<\/li>\n<li>\u5982\u679c\u6709\uff0c\u5219\u68c0\u67e5\u65f6\u95f4\u6233\uff0c\u4ee5\u786e\u8ba4\u662f\u5426\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u3002<\/li>\n<li>\u5982\u679c\u6ca1\u6709<code>.pyc<\/code>\u6587\u4ef6\u6216\u6e90\u4ee3\u7801\u6709\u66f4\u6539\uff0c\u5219\u91cd\u65b0\u7f16\u8bd1\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4f18\u7f3a\u70b9<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u65e0\u9700\u624b\u52a8\u5e72\u9884\uff0c\u81ea\u52a8\u5316\u5904\u7406\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u65e0\u6cd5\u63a7\u5236\u751f\u6210\u65f6\u673a\uff0c\u4ec5\u5728\u6a21\u5757\u5bfc\u5165\u65f6\u751f\u6210\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528COMPILEALL\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>compileall<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u6279\u91cf\u7f16\u8bd1\u76ee\u5f55\u4e2d\u6240\u6709Python\u6587\u4ef6\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u6574\u4e2a\u9879\u76ee\u8fdb\u884c\u7f16\u8bd1\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u8bd1\u76ee\u5f55<\/strong><br \/>\u4f7f\u7528<code>compileall<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u7f16\u8bd1\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709<code>.py<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m compileall &lt;directory&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u904d\u5386\u6307\u5b9a\u76ee\u5f55\u53ca\u5176\u5b50\u76ee\u5f55\uff0c\u7f16\u8bd1\u6240\u6709\u7684<code>.py<\/code>\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5177\u4f53\u64cd\u4f5c<\/strong><br \/>\u4f8b\u5982\uff0c\u8981\u7f16\u8bd1\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6240\u6709Python\u6587\u4ef6\uff0c\u53ef\u4ee5\u6267\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m compileall .<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4f1a\u751f\u6210<code>__pycache__<\/code>\u76ee\u5f55\uff0c\u5e76\u5728\u5176\u4e2d\u5b58\u653e\u7f16\u8bd1\u540e\u7684<code>.pyc<\/code>\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u7f3a\u70b9<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u9002\u5408\u6279\u91cf\u5904\u7406\uff0c\u8282\u7701\u65f6\u95f4\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u9700\u8981\u624b\u52a8\u6267\u884c\u547d\u4ee4\uff0c\u4e0d\u80fd\u9009\u62e9\u6027\u7f16\u8bd1\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528PY_COMPILE\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>py_compile<\/code>\u6a21\u5757\u7528\u4e8e\u7f16\u8bd1\u5355\u4e2aPython\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u624b\u52a8\u63a7\u5236\u7f16\u8bd1\u8fc7\u7a0b\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u8bd1\u5355\u4e2a\u6587\u4ef6<\/strong><br \/>\u4f7f\u7528<code>py_compile<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u7f16\u8bd1\u5355\u4e2aPython\u6587\u4ef6\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\u5c06\u521b\u5efa\u4e00\u4e2a\u7f16\u8bd1\u540e\u7684<code>.pyc<\/code>\u6587\u4ef6\uff0c\u5b58\u50a8\u5728<code>__pycache__<\/code>\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5177\u4f53\u5e94\u7528<\/strong><br \/>\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a<code>example.py<\/code>\u7684\u6587\u4ef6\uff0c\u9700\u8981\u7f16\u8bd1\u5b83\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u4ee3\u7801\u5728Python\u811a\u672c\u4e2d\u6267\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u7f3a\u70b9<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u7f16\u8bd1\u5355\u4e2a\u6587\u4ef6\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u5bf9\u4e8e\u5927\u91cf\u6587\u4ef6\u9700\u8981\u591a\u6b21\u8c03\u7528\uff0c\u6548\u7387\u8f83\u4f4e\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001PYC\u6587\u4ef6\u7684\u4f5c\u7528\u4e0e\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u63d0\u9ad8\u7a0b\u5e8f\u52a0\u8f7d\u901f\u5ea6<\/strong><br \/><code>.pyc<\/code>\u6587\u4ef6\u662f\u7f16\u8bd1\u540e\u7684\u5b57\u8282\u7801\u6587\u4ef6\uff0c\u52a0\u8f7d\u901f\u5ea6\u6bd4\u6e90\u4ee3\u7801\u5feb\uff0c\u56e0\u4e3a\u7701\u53bb\u4e86\u7f16\u8bd1\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u62a4\u6e90\u4ee3\u7801<\/strong><br \/>\u5206\u53d1\u7a0b\u5e8f\u65f6\u53ef\u4ee5\u53ea\u63d0\u4f9b<code>.pyc<\/code>\u6587\u4ef6\uff0c\u800c\u4e0d\u9700\u8981\u63d0\u4f9b\u6e90\u4ee3\u7801\uff0c\u4ece\u800c\u4fdd\u62a4\u4ee3\u7801\u4e0d\u88ab\u8f7b\u6613\u67e5\u770b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u90e8\u7f72\u548c\u53d1\u5e03<\/strong><br \/>\u5728\u8f6f\u4ef6\u53d1\u5e03\u65f6\uff0c\u53ef\u4ee5\u5c06<code>.pyc<\/code>\u6587\u4ef6\u6253\u5305\uff0c\u800c\u4e0d\u63d0\u4f9b<code>.py<\/code>\u6587\u4ef6\uff0c\u65e2\u4fdd\u62a4\u4e86\u4ee3\u7801\u53c8\u63d0\u9ad8\u4e86\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u517c\u5bb9\u6027\u95ee\u9898<\/strong><br \/><code>.pyc<\/code>\u6587\u4ef6\u4e0ePython\u7248\u672c\u7d27\u5bc6\u76f8\u5173\uff0c\u4e0d\u540c\u7248\u672c\u53ef\u80fd\u4e0d\u517c\u5bb9\u3002\u56e0\u6b64\uff0c\u5728\u4e0d\u540cPython\u73af\u5883\u4e2d\u9700\u8981\u91cd\u65b0\u751f\u6210\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u8bd5\u95ee\u9898<\/strong><br \/>\u8c03\u8bd5\u65f6\uff0c\u6e90\u4ee3\u7801\u4e0e\u5b57\u8282\u7801\u53ef\u80fd\u4e0d\u4e00\u81f4\uff0c\u9700\u786e\u4fdd<code>.pyc<\/code>\u6587\u4ef6\u662f\u6700\u65b0\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u76ee\u5f55\u7ba1\u7406<\/strong><br \/>\u5efa\u8bae\u4f7f\u7528\u7248\u672c\u63a7\u5236\u5de5\u5177\u5ffd\u7565<code>__pycache__<\/code>\u76ee\u5f55\uff0c\u4ee5\u514d\u4e0d\u5fc5\u8981\u7684\u6587\u4ef6\u6df7\u5165\u9879\u76ee\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u5e94\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u5f0f\u751f\u6210<code>.pyc<\/code>\u6587\u4ef6\uff0c\u4ece\u800c\u63d0\u9ad8Python\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u548c\u4ee3\u7801\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u751f\u6210pyc\u6587\u4ef6\u7684\u76ee\u7684\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>pyc\u6587\u4ef6\u662fPython\u7f16\u8bd1\u540e\u7684\u5b57\u8282\u7801\u6587\u4ef6\uff0c\u4e3b\u8981\u7528\u4e8e\u63d0\u9ad8\u7a0b\u5e8f\u7684\u542f\u52a8\u901f\u5ea6\u3002\u901a\u8fc7\u5c06Python\u6e90\u4ee3\u7801(.py)\u7f16\u8bd1\u4e3a\u5b57\u8282\u7801(.pyc)\uff0cPython\u89e3\u91ca\u5668\u53ef\u4ee5\u76f4\u63a5\u52a0\u8f7d\u5b57\u8282\u7801\uff0c\u907f\u514d\u4e86\u6bcf\u6b21\u8fd0\u884c\u65f6\u90fd\u8fdb\u884c\u7f16\u8bd1\u7684\u8fc7\u7a0b\u3002\u8fd9\u79cd\u673a\u5236\u5728\u6a21\u5757\u88ab\u591a\u6b21\u5bfc\u5165\u65f6\u5c24\u5176\u6709\u6548\u3002<\/p>\n<p><strong>\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f1a\u81ea\u52a8\u751f\u6210pyc\u6587\u4ef6\uff1f<\/strong><br \/>\u5f53\u4f60\u5728\u6267\u884c\u4e00\u4e2aPython\u7a0b\u5e8f\u65f6\uff0c\u5982\u679c\u8be5\u7a0b\u5e8f\u5bfc\u5165\u4e86\u5176\u4ed6\u6a21\u5757\uff0cPython\u4f1a\u81ea\u52a8\u68c0\u67e5\u5bf9\u5e94\u7684pyc\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0cPython\u4f1a\u7f16\u8bd1\u6e90\u6587\u4ef6\u5e76\u751f\u6210pyc\u6587\u4ef6\u3002\u5982\u679c\u6e90\u6587\u4ef6\u7684\u4fee\u6539\u65f6\u95f4\u6bd4pyc\u6587\u4ef6\u7684\u65b0\uff0cPython\u4e5f\u4f1a\u91cd\u65b0\u7f16\u8bd1\u751f\u6210\u65b0\u7684pyc\u6587\u4ef6\u3002\u8fd9\u4e00\u8fc7\u7a0b\u901a\u5e38\u662f\u900f\u660e\u7684\uff0c\u7528\u6237\u65e0\u9700\u624b\u52a8\u5e72\u9884\u3002<\/p>\n<p><strong>\u5982\u4f55\u624b\u52a8\u7f16\u8bd1.py\u6587\u4ef6\u4e3a.pyc\u6587\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>py_compile<\/code>\u6a21\u5757\u624b\u52a8\u751f\u6210pyc\u6587\u4ef6\u3002\u901a\u8fc7\u547d\u4ee4\u884c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a  <\/p>\n<pre><code class=\"language-python\">import py_compile\npy_compile.compile(&#39;your_script.py&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6761\u547d\u4ee4\u4f1a\u5728\u4e0e\u6e90\u6587\u4ef6\u76f8\u540c\u7684\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a__pycache__\u6587\u4ef6\u5939\uff0c\u91cc\u9762\u5305\u542b\u7f16\u8bd1\u597d\u7684pyc\u6587\u4ef6\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>compileall<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u6279\u91cf\u7f16\u8bd1\u76ee\u5f55\u4e0b\u7684\u6240\u6709.py\u6587\u4ef6\u4e3a.pyc\u6587\u4ef6\uff0c\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u751f\u6210pyc\u6587\u4ef6\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u81ea\u52a8\u751f\u6210\u3001\u4f7f\u7528compileall\u6a21\u5757\u3001\u4f7f\u7528py_compile\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":922121,"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\/922115"}],"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=922115"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/922115\/revisions"}],"predecessor-version":[{"id":922122,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/922115\/revisions\/922122"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/922121"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=922115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=922115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=922115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}