{"id":944008,"date":"2024-12-26T22:58:32","date_gmt":"2024-12-26T14:58:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/944008.html"},"modified":"2024-12-26T22:58:34","modified_gmt":"2024-12-26T14:58:34","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8pyc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/944008.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528pyc"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081203\/915589d7-9db1-4306-9890-175f65c6c8c8.webp\" alt=\"python\u5982\u4f55\u8c03\u7528pyc\" \/><\/p>\n<p><p> <strong>Python\u8c03\u7528PYC\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u76f4\u63a5\u5bfc\u5165PYC\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u3001\u4f7f\u7528<code>importlib<\/code>\u6a21\u5757\u52a8\u6001\u52a0\u8f7d\u3001\u786e\u4fdd\u6b63\u786e\u7684Python\u7248\u672c\u5339\u914d\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u76f4\u63a5\u5bfc\u5165\u662f\u6700\u5e38\u89c1\u548c\u6700\u7b80\u5355\u7684\u65b9\u5f0f\uff0c\u800c\u4f7f\u7528<code>importlib<\/code>\u53ef\u4ee5\u63d0\u4f9b\u66f4\u591a\u7684\u7075\u6d3b\u6027\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u76f4\u63a5\u5bfc\u5165PYC\u6587\u4ef6<\/p>\n<\/p>\n<p><p>PYC\u6587\u4ef6\u662fPython\u7f16\u8bd1\u540e\u7684\u5b57\u8282\u7801\u6587\u4ef6\uff0c\u901a\u5e38\u4f4d\u4e8e\u4e0e\u6e90\u4ee3\u7801\u76f8\u540c\u7684\u76ee\u5f55\u4e0b\u6216\u8005<code>__pycache__<\/code>\u6587\u4ef6\u5939\u4e2d\u3002\u8981\u8c03\u7528PYC\u6587\u4ef6\uff0c\u9996\u5148\u786e\u4fdd\u5b83\u662f\u7531\u76f8\u540c\u7248\u672c\u7684Python\u89e3\u91ca\u5668\u751f\u6210\u7684\uff0c\u56e0\u4e3a\u4e0d\u540c\u7248\u672c\u4e4b\u95f4\u7684\u5b57\u8282\u7801\u53ef\u80fd\u4e0d\u517c\u5bb9\u3002\u5bfc\u5165PYC\u6587\u4ef6\u7684\u65b9\u6cd5\u4e0e\u5bfc\u5165PY\u6587\u4ef6\u76f8\u540c\uff0cPython\u4f1a\u81ea\u52a8\u8bc6\u522b\u5e76\u52a0\u8f7dPYC\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u786e\u4fddPYC\u6587\u4ef6\u7684\u751f\u6210<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u5e38\uff0c\u5f53\u4f60\u7b2c\u4e00\u6b21\u5bfc\u5165\u4e00\u4e2aPython\u6a21\u5757\u65f6\uff0cPython\u4f1a\u81ea\u52a8\u7f16\u8bd1\u8be5\u6a21\u5757\uff0c\u5e76\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2aPYC\u6587\u4ef6\u3002\u5982\u679c\u4f60\u624b\u52a8\u5220\u9664\u4e86PYC\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u8be5\u6a21\u5757\u518d\u6b21\u751f\u6210\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bfc\u5165PYC\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>mymodule.py<\/code>\u7684\u6a21\u5757\uff0c\u5bfc\u5165\u6b64\u6a21\u5757\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5373\u4fbf\u6e90\u6587\u4ef6\u88ab\u5220\u9664\uff0c\u53ea\u8981PYC\u6587\u4ef6\u5b58\u5728\uff0cPython\u4ecd\u7136\u53ef\u4ee5\u5bfc\u5165\u5b83\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>importlib<\/code>\u6a21\u5757\u52a8\u6001\u52a0\u8f7d<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u52a8\u6001\u5730\u52a0\u8f7dPYC\u6587\u4ef6\u3002Python\u7684<code>importlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8fd9\u79cd\u7075\u6d3b\u6027\uff0c\u8ba9\u4f60\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u51b3\u5b9a\u8981\u52a0\u8f7d\u54ea\u4e2a\u6a21\u5757\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528<code>importlib.util.spec_from_file_location<\/code><\/strong><\/p>\n<\/p>\n<p><p><code>importlib.util.spec_from_file_location<\/code>\u53ef\u4ee5\u4ece\u6587\u4ef6\u8def\u5f84\u52a0\u8f7d\u6a21\u5757\uff0c\u8fd9\u5728\u9700\u8981\u52a8\u6001\u6307\u5b9a\u6a21\u5757\u8def\u5f84\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib.util<\/p>\n<p>module_name = &#39;mymodule&#39;<\/p>\n<p>file_path = &#39;\/path\/to\/__pycache__\/mymodule.cpython-38.pyc&#39;<\/p>\n<p>spec = importlib.util.spec_from_file_location(module_name, file_path)<\/p>\n<p>mymodule = importlib.util.module_from_spec(spec)<\/p>\n<p>spec.loader.exec_module(mymodule)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u52a0\u8f7d\u6a21\u5757\u540e\uff0c\u4f60\u53ef\u4ee5\u50cf\u4f7f\u7528\u666e\u901a\u6a21\u5757\u4e00\u6837\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\u6216\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = mymodule.some_function()<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u786e\u4fdd\u6b63\u786e\u7684Python\u7248\u672c\u5339\u914d<\/p>\n<\/p>\n<p><p>PYC\u6587\u4ef6\u662f\u7279\u5b9aPython\u7248\u672c\u751f\u6210\u7684\u5b57\u8282\u7801\uff0c\u4f7f\u7528\u4e0d\u5339\u914d\u7684Python\u7248\u672c\u53ef\u80fd\u5bfc\u81f4\u5bfc\u5165\u5931\u8d25\u6216\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u786e\u4fdd\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7248\u672c\u4e00\u81f4\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u751f\u6210PYC\u6587\u4ef6\u548c\u8fd0\u884cPYC\u6587\u4ef6\u7684Python\u7248\u672c\u5fc5\u987b\u4e00\u81f4\u3002\u5982\u679c\u4f60\u66f4\u65b0\u4e86Python\u7248\u672c\uff0c\u6700\u597d\u91cd\u65b0\u751f\u6210PYC\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u6587\u4ef6\u540d<\/strong><\/p>\n<\/p>\n<p><p>\u5728<code>__pycache__<\/code>\u76ee\u5f55\u4e0b\uff0cPYC\u6587\u4ef6\u7684\u547d\u540d\u683c\u5f0f\u901a\u5e38\u4e3a<code>module_name.cpython-XY.pyc<\/code>\uff0c\u5176\u4e2d<code>XY<\/code>\u8868\u793aPython\u7684\u7248\u672c\u53f7\uff08\u4f8b\u5982\uff0c38\u8868\u793aPython 3.8\uff09\u3002\u786e\u4fdd\u4f60\u7684Python\u89e3\u91ca\u5668\u7248\u672c\u4e0ePYC\u6587\u4ef6\u540d\u4e2d\u7684\u7248\u672c\u53f7\u5339\u914d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u91cd\u65b0\u7f16\u8bd1<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u53d1\u73b0\u7248\u672c\u4e0d\u5339\u914d\uff0c\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u7f16\u8bd1\u6e90\u6587\u4ef6\u6765\u751f\u6210\u9002\u5408\u5f53\u524dPython\u7248\u672c\u7684PYC\u6587\u4ef6\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m py_compile mymodule.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5904\u7406PYC\u6587\u4ef6\u7684\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u4f7f\u7528PYC\u6587\u4ef6\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5904\u7406PYC\u6587\u4ef6\u7684\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7248\u672c\u63a7\u5236<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u5e38\uff0cPYC\u6587\u4ef6\u4e0d\u9700\u8981\u88ab\u7eb3\u5165\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u53ef\u518d\u751f\u7684\u3002\u5982\u679c\u9700\u8981\u4fdd\u7559\u7f16\u8bd1\u540e\u7684\u5b57\u8282\u7801\uff0c\u786e\u4fdd\u6bcf\u6b21\u4ee3\u7801\u66f4\u6539\u540e\u90fd\u91cd\u65b0\u7f16\u8bd1\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u76ee\u5f55\u7ed3\u6784<\/strong><\/p>\n<\/p>\n<p><p>\u5c06PYC\u6587\u4ef6\u7ec4\u7ec7\u5728<code>__pycache__<\/code>\u76ee\u5f55\u4e2d\uff0c\u4fdd\u6301\u9879\u76ee\u7684\u6574\u6d01\u6027\u3002Python\u4f1a\u81ea\u52a8\u7ba1\u7406\u8fd9\u4e9b\u76ee\u5f55\uff0c\u65e0\u9700\u624b\u52a8\u5e72\u9884\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u8bd5<\/strong><\/p>\n<\/p>\n<p><p>\u8c03\u8bd5PYC\u6587\u4ef6\u53ef\u80fd\u4f1a\u6709\u4e9b\u56f0\u96be\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u7f16\u8bd1\u540e\u7684\u5b57\u8282\u7801\u3002\u4e3a\u4e86\u4fbf\u4e8e\u8c03\u8bd5\uff0c\u5efa\u8bae\u4fdd\u7559\u6e90\u4ee3\u7801\u6587\u4ef6\uff08.py\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u8c03\u7528PYC\u6587\u4ef6\u867d\u7136\u7b80\u5355\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7248\u672c\u517c\u5bb9\u6027\u548c\u6b63\u786e\u7684\u6587\u4ef6\u8def\u5f84\u3002\u4f7f\u7528\u76f4\u63a5\u5bfc\u5165\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u800c<code>importlib<\/code>\u63d0\u4f9b\u4e86\u52a8\u6001\u52a0\u8f7d\u7684\u7075\u6d3b\u6027\u3002\u786e\u4fddPython\u7248\u672c\u5339\u914d\u548c\u59a5\u5584\u7ba1\u7406PYC\u6587\u4ef6\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u4f7f\u7528Python\u7f16\u8bd1\u7684\u5b57\u8282\u7801\u6587\u4ef6\u3002\u901a\u8fc7\u5bf9\u4e0a\u8ff0\u65b9\u6cd5\u7684\u7406\u89e3\u548c\u5b9e\u8df5\uff0c\u4f60\u80fd\u591f\u66f4\u597d\u5730\u5728\u9879\u76ee\u4e2d\u7ba1\u7406\u548c\u8c03\u7528PYC\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u52a0\u8f7d\u548c\u8fd0\u884cpyc\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0cpyc\u6587\u4ef6\u662f\u7ecf\u8fc7\u7f16\u8bd1\u7684\u5b57\u8282\u7801\u6587\u4ef6\uff0c\u901a\u5e38\u7531Python\u89e3\u91ca\u5668\u81ea\u52a8\u751f\u6210\u3002\u8981\u52a0\u8f7d\u548c\u8fd0\u884cpyc\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u76f4\u63a5\u5bfc\u5165\u5305\u542bpyc\u6587\u4ef6\u7684\u6a21\u5757\u3002\u786e\u4fddpyc\u6587\u4ef6\u4e0e\u6e90\u4ee3\u7801\u6587\u4ef6\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u5e76\u4e14Python\u89e3\u91ca\u5668\u53ef\u4ee5\u627e\u5230\u5b83\u3002\u5728\u5bfc\u5165\u6a21\u5757\u65f6\uff0cPython\u4f1a\u81ea\u52a8\u67e5\u627e\u5e76\u52a0\u8f7d\u76f8\u5e94\u7684pyc\u6587\u4ef6\u3002<\/p>\n<p><strong>pyc\u6587\u4ef6\u4e0e\u6e90\u4ee3\u7801\u6587\u4ef6\u4e4b\u95f4\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>pyc\u6587\u4ef6\u662fPython\u6e90\u4ee3\u7801\uff08.py\u6587\u4ef6\uff09\u7f16\u8bd1\u540e\u751f\u6210\u7684\u5b57\u8282\u7801\u6587\u4ef6\u3002\u6e90\u4ee3\u7801\u6587\u4ef6\u662f\u4eba\u7c7b\u53ef\u8bfb\u7684\uff0c\u800cpyc\u6587\u4ef6\u662f\u673a\u5668\u53ef\u8bfb\u7684\uff0c\u901a\u5e38\u7528\u4e8e\u63d0\u9ad8\u7a0b\u5e8f\u7684\u52a0\u8f7d\u901f\u5ea6\u3002pyc\u6587\u4ef6\u5728\u6267\u884c\u65f6\u4f1a\u88abPython\u89e3\u91ca\u5668\u52a0\u8f7d\uff0c\u800c\u6e90\u4ee3\u7801\u6587\u4ef6\u5219\u9700\u8981\u5728\u6bcf\u6b21\u8fd0\u884c\u65f6\u88ab\u89e3\u6790\u548c\u7f16\u8bd1\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\u7f16\u8bd1.py\u6587\u4ef6\u4e3a.pyc\u6587\u4ef6\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\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\u5c06\u751f\u6210\u5bf9\u5e94\u7684pyc\u6587\u4ef6\uff0c\u901a\u5e38\u4f4d\u4e8e<code>__pycache__<\/code>\u76ee\u5f55\u4e2d\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u624b\u52a8\u7f16\u8bd1\u548c\u4f18\u5316Python\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528PYC\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u76f4\u63a5\u5bfc\u5165PYC\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u3001\u4f7f\u7528importlib\u6a21\u5757\u52a8\u6001\u52a0\u8f7d\u3001\u786e\u4fdd\u6b63\u786e [&hellip;]","protected":false},"author":3,"featured_media":944010,"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\/944008"}],"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=944008"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/944008\/revisions"}],"predecessor-version":[{"id":944013,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/944008\/revisions\/944013"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/944010"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=944008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=944008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=944008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}