{"id":1169423,"date":"2025-01-15T16:10:24","date_gmt":"2025-01-15T08:10:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1169423.html"},"modified":"2025-01-15T16:10:28","modified_gmt":"2025-01-15T08:10:28","slug":"python%e5%a6%82%e4%bd%95%e8%bd%ac%e6%8d%a2%e4%b8%bapyc%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1169423.html","title":{"rendered":"python\u5982\u4f55\u8f6c\u6362\u4e3apyc\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070407\/49cd89f3-55ac-4651-bc77-11c2e5a4cb21.webp\" alt=\"python\u5982\u4f55\u8f6c\u6362\u4e3apyc\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u8f6c\u6362\u4e3apyc\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u7684 <code>compileall<\/code> \u6a21\u5757\u3001\u4f7f\u7528 <code>py_compile<\/code> \u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u5982 <code>Cython<\/code>\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528 <code>compileall<\/code> \u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>compileall<\/code> \u6a21\u5757\u6765\u751f\u6210 <code>.pyc<\/code> \u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002<code>compileall<\/code> \u662f Python \u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u6279\u91cf\u7f16\u8bd1 <code>.py<\/code> \u6587\u4ef6\u4e3a <code>.pyc<\/code> \u6587\u4ef6\u7684\u529f\u80fd\u3002\u4e0b\u9762\u662f\u5177\u4f53\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u786e\u4fddPython\u5b89\u88c5\u6b63\u786e\u5e76\u53ef\u7528<\/strong>\uff1a\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165 <code>python --version<\/code> \u68c0\u67e5 Python \u662f\u5426\u5b89\u88c5\u6b63\u786e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u6216\u627e\u5230\u76ee\u6807 .py \u6587\u4ef6\u6216\u76ee\u5f55<\/strong>\uff1a\u786e\u5b9a\u4f60\u8981\u8f6c\u6362\u7684 .py \u6587\u4ef6\u6216\u5305\u542b\u591a\u4e2a .py \u6587\u4ef6\u7684\u76ee\u5f55\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c compileall \u6a21\u5757<\/strong>\uff1a\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8fd0\u884c <code>compileall<\/code> \u6a21\u5757\u6765\u7f16\u8bd1\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709 .py \u6587\u4ef6\u3002<\/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>\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a .py \u6587\u4ef6\u7684\u76ee\u5f55 <code>my_project<\/code>\uff0c\u4f60\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m compileall my_project<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u751f\u6210\u7684 .pyc \u6587\u4ef6<\/strong>\uff1a\u6210\u529f\u8fd0\u884c\u540e\uff0cPython \u4f1a\u5728\u4e0e .py \u6587\u4ef6\u5bf9\u5e94\u7684 <code>__pycache__<\/code> \u76ee\u5f55\u4e2d\u751f\u6210 <code>.pyc<\/code> \u6587\u4ef6\u3002\u8fd9\u4e9b .pyc \u6587\u4ef6\u662f\u7f16\u8bd1\u540e\u7684\u5b57\u8282\u7801\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e00\u3001\u4e3a\u4ec0\u4e48\u8981\u8f6c\u6362\u4e3a .pyc \u6587\u4ef6\uff1f<\/h3>\n<\/p>\n<p><p>\u8f6c\u6362 .py \u6587\u4ef6\u4e3a .pyc \u6587\u4ef6\u6709\u51e0\u4e2a\u660e\u663e\u7684\u597d\u5904\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u63d0\u5347\u7a0b\u5e8f\u8fd0\u884c\u901f\u5ea6<\/strong>\uff1a<code>.pyc<\/code> \u6587\u4ef6\u662f Python \u6e90\u4ee3\u7801\u7684\u7f16\u8bd1\u5b57\u8282\u7801\u6587\u4ef6\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u88ab Python \u89e3\u91ca\u5668\u6267\u884c\uff0c\u800c\u65e0\u9700\u518d\u6b21\u7f16\u8bd1\u3002\u8fd9\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u542f\u52a8\u901f\u5ea6\u3002<\/li>\n<li><strong>\u4fdd\u62a4\u6e90\u4ee3\u7801<\/strong>\uff1a\u53d1\u5e03 <code>.pyc<\/code> \u6587\u4ef6\u800c\u4e0d\u662f <code>.py<\/code> \u6587\u4ef6\u53ef\u4ee5\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u4fdd\u62a4\u4f60\u7684\u6e90\u4ee3\u7801\u4e0d\u88ab\u968f\u610f\u67e5\u770b\u548c\u4fee\u6539\u3002<\/li>\n<li><strong>\u51cf\u5c11\u89e3\u6790\u65f6\u95f4<\/strong>\uff1aPython \u89e3\u91ca\u5668\u4e0d\u9700\u8981\u5728\u6bcf\u6b21\u6267\u884c\u65f6\u91cd\u65b0\u89e3\u6790\u6e90\u4ee3\u7801\uff0c\u53ef\u4ee5\u76f4\u63a5\u52a0\u8f7d\u7f16\u8bd1\u597d\u7684 <code>.pyc<\/code> \u6587\u4ef6\uff0c\u4ece\u800c\u51cf\u5c11\u89e3\u6790\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 py_compile \u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u9664\u4e86 <code>compileall<\/code>\uff0cPython \u8fd8\u63d0\u4f9b\u4e86 <code>py_compile<\/code> \u6a21\u5757\u6765\u7f16\u8bd1\u5355\u4e2a .py \u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 <code>py_compile<\/code> \u6a21\u5757\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u5199\u4e00\u4e2a Python \u811a\u672c<\/strong>\uff1a\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a Python \u811a\u672c\u6765\u7f16\u8bd1\u6307\u5b9a\u7684 .py \u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import py_compile<\/p>\n<p>py_compile.compile(&#39;your_script.py&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u811a\u672c<\/strong>\uff1a\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8fd0\u884c\u8fd9\u4e2a\u811a\u672c\uff0c<code>your_script.py<\/code> \u6587\u4ef6\u5c06\u88ab\u7f16\u8bd1\u4e3a <code>your_script.cpython-&lt;version&gt;.pyc<\/code> \u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python compile_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177 Cython<\/h3>\n<\/p>\n<p><p><code>Cython<\/code> \u662f\u4e00\u4e2a\u5c06 Python \u4ee3\u7801\u7f16\u8bd1\u4e3a C \u4ee3\u7801\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002\u4f7f\u7528 <code>Cython<\/code> \u7f16\u8bd1 Python \u4ee3\u7801\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5 Cython<\/strong>\uff1a\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5 <code>Cython<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install cython<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u4e00\u4e2a setup.py \u6587\u4ef6<\/strong>\uff1a\u8fd9\u4e2a\u6587\u4ef6\u7528\u4e8e\u914d\u7f6e <code>Cython<\/code> \u7f16\u8bd1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from distutils.core import setup<\/p>\n<p>from Cython.Build import cythonize<\/p>\n<p>setup(<\/p>\n<p>    ext_modules = cythonize(&quot;your_script.py&quot;)<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c setup.py<\/strong>\uff1a\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8fd0\u884c <code>setup.py<\/code> \u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py build_ext --inplace<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u4e0e <code>your_script.py<\/code> \u76f8\u5bf9\u5e94\u7684 <code>.so<\/code> \u6216 <code>.pyd<\/code> \u6587\u4ef6\uff08\u5177\u4f53\u53d6\u51b3\u4e8e\u64cd\u4f5c\u7cfb\u7edf\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u6df1\u5165\u7406\u89e3 .pyc \u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>1\u3001.pyc \u6587\u4ef6\u7684\u7ed3\u6784<\/h4>\n<\/p>\n<p><p><code>.pyc<\/code> \u6587\u4ef6\u662f Python \u6e90\u4ee3\u7801\u7684\u7f16\u8bd1\u5b57\u8282\u7801\u6587\u4ef6\uff0c\u5b83\u7684\u7ed3\u6784\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u9b54\u6570\uff08Magic Number\uff09<\/strong>\uff1a\u524d 4 \u4e2a\u5b57\u8282\u662f\u9b54\u6570\uff0c\u7528\u4e8e\u6807\u8bc6\u6587\u4ef6\u683c\u5f0f\u548c\u7248\u672c\u3002<\/li>\n<li><strong>\u65f6\u95f4\u6233<\/strong>\uff1a\u63a5\u4e0b\u6765\u7684 4 \u4e2a\u5b57\u8282\u662f\u65f6\u95f4\u6233\uff0c\u7528\u4e8e\u8bb0\u5f55\u6e90\u6587\u4ef6\u7684\u6700\u540e\u4fee\u6539\u65f6\u95f4\u3002<\/li>\n<li><strong>\u6e90\u4ee3\u7801\u5927\u5c0f<\/strong>\uff1a\u63a5\u4e0b\u6765\u7684 4 \u4e2a\u5b57\u8282\u8bb0\u5f55\u6e90\u6587\u4ef6\u7684\u5927\u5c0f\uff08Python 3.7 \u53ca\u4ee5\u4e0a\u7248\u672c\uff09\u3002<\/li>\n<li><strong>\u5b57\u8282\u7801<\/strong>\uff1a\u5269\u4e0b\u7684\u90e8\u5206\u662f\u7f16\u8bd1\u540e\u7684\u5b57\u8282\u7801\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001\u5982\u4f55\u52a0\u8f7d .pyc \u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Python \u89e3\u91ca\u5668\u5728\u5bfc\u5165\u6a21\u5757\u65f6\uff0c\u4f1a\u81ea\u52a8\u67e5\u627e\u548c\u52a0\u8f7d <code>.pyc<\/code> \u6587\u4ef6\u3002\u5982\u679c\u5b58\u5728\u4e0e <code>.py<\/code> \u6587\u4ef6\u5bf9\u5e94\u7684 <code>.pyc<\/code> \u6587\u4ef6\uff0c\u5e76\u4e14 <code>.pyc<\/code> \u6587\u4ef6\u7684\u65f6\u95f4\u6233\u4e0e <code>.py<\/code> \u6587\u4ef6\u5339\u914d\uff0c\u89e3\u91ca\u5668\u5c06\u76f4\u63a5\u52a0\u8f7d <code>.pyc<\/code> \u6587\u4ef6\uff0c\u800c\u4e0d\u662f\u91cd\u65b0\u7f16\u8bd1 <code>.py<\/code> \u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>3\u3001.pyc \u6587\u4ef6\u7684\u517c\u5bb9\u6027<\/h4>\n<\/p>\n<p><p><code>.pyc<\/code> \u6587\u4ef6\u4e0e\u7279\u5b9a\u7248\u672c\u7684 Python \u89e3\u91ca\u5668\u76f8\u5173\u8054\uff0c\u56e0\u6b64\u5728\u4e0d\u540c\u7248\u672c\u7684 Python \u89e3\u91ca\u5668\u4e4b\u95f4\u53ef\u80fd\u4e0d\u517c\u5bb9\u3002\u4f8b\u5982\uff0c\u7531 Python 3.8 \u751f\u6210\u7684 <code>.pyc<\/code> \u6587\u4ef6\u53ef\u80fd\u65e0\u6cd5\u5728 Python 3.7 \u89e3\u91ca\u5668\u4e2d\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f18\u5316 .pyc \u6587\u4ef6\u7684\u751f\u6210\u548c\u4f7f\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528 <code>-O<\/code> \u548c <code>-OO<\/code> \u9009\u9879<\/h4>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86 <code>-O<\/code> \u548c <code>-OO<\/code> \u9009\u9879\u6765\u4f18\u5316\u751f\u6210\u7684 <code>.pyc<\/code> \u6587\u4ef6\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong><code>-O<\/code> \u9009\u9879<\/strong>\uff1a\u4f18\u5316\u7ea7\u522b 1\uff0c\u751f\u6210\u7684 <code>.pyc<\/code> \u6587\u4ef6\u4f1a\u5ffd\u7565\u65ad\u8a00\uff08assert\uff09\u8bed\u53e5\u3002<\/li>\n<li><strong><code>-OO<\/code> \u9009\u9879<\/strong>\uff1a\u4f18\u5316\u7ea7\u522b 2\uff0c\u751f\u6210\u7684 <code>.pyc<\/code> \u6587\u4ef6\u4e0d\u4ec5\u5ffd\u7565\u65ad\u8a00\u8bed\u53e5\uff0c\u8fd8\u4f1a\u5220\u9664\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\u3002<\/li>\n<\/ul>\n<p><p>\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u751f\u6210\u4f18\u5316\u540e\u7684 <code>.pyc<\/code> \u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -O -m compileall &lt;directory&gt;<\/p>\n<p>python -OO -m compileall &lt;directory&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6279\u91cf\u751f\u6210 .pyc \u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u811a\u672c\u6765\u6279\u91cf\u751f\u6210 <code>.pyc<\/code> \u6587\u4ef6\u3002\u8fd9\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u7684 <code>.py<\/code> \u6587\u4ef6\u90fd\u88ab\u7f16\u8bd1\uff0c\u5e76\u4e14\u4e0d\u4f1a\u9057\u6f0f\u4efb\u4f55\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import compileall<\/p>\n<p>import os<\/p>\n<p>def compile_directory(directory):<\/p>\n<p>    for root, dirs, files in os.walk(directory):<\/p>\n<p>        for file in files:<\/p>\n<p>            if file.endswith(&#39;.py&#39;):<\/p>\n<p>                compileall.compile_file(os.path.join(root, file))<\/p>\n<p>compile_directory(&#39;my_project&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u4e2a\u811a\u672c\u5c06\u9012\u5f52\u5730\u7f16\u8bd1 <code>my_project<\/code> \u76ee\u5f55\u4e0b\u7684\u6240\u6709 <code>.py<\/code> \u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u8df5\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1\u3001\u7248\u672c\u63a7\u5236<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08\u5982 Git\uff09\u65f6\uff0c\u5e94\u907f\u514d\u5c06 <code>.pyc<\/code> \u6587\u4ef6\u6dfb\u52a0\u5230\u7248\u672c\u5e93\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528 <code>.gitignore<\/code> \u6587\u4ef6\u6765\u5ffd\u7565 <code>.pyc<\/code> \u6587\u4ef6\u548c <code>__pycache__<\/code> \u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code>__pycache__\/<\/p>\n<p>*.pyc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u90e8\u7f72\u548c\u5206\u53d1<\/h4>\n<\/p>\n<p><p>\u5728\u90e8\u7f72\u548c\u5206\u53d1 Python \u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u53ea\u53d1\u5e03 <code>.pyc<\/code> \u6587\u4ef6\u800c\u4e0d\u5305\u62ec <code>.py<\/code> \u6587\u4ef6\u3002\u8fd9\u53ef\u4ee5\u4fdd\u62a4\u6e90\u4ee3\u7801\uff0c\u5e76\u4e14\u51cf\u5c11\u6700\u7ec8\u7528\u6237\u7684\u89e3\u6790\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u8c03\u8bd5\u548c\u5f00\u53d1<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u9636\u6bb5\uff0c\u901a\u5e38\u9700\u8981\u4fdd\u7559 <code>.py<\/code> \u6587\u4ef6\u4ee5\u4fbf\u4e8e\u67e5\u770b\u548c\u4fee\u6539\u6e90\u4ee3\u7801\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5b9a\u671f\u8fd0\u884c <code>compileall<\/code> \u6216 <code>py_compile<\/code> \u6765\u751f\u6210 <code>.pyc<\/code> \u6587\u4ef6\uff0c\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6df1\u5165\u7406\u89e3 Python \u5b57\u8282\u7801<\/h3>\n<\/p>\n<p><h4>1\u3001\u4ec0\u4e48\u662f Python \u5b57\u8282\u7801<\/h4>\n<\/p>\n<p><p>Python \u5b57\u8282\u7801\u662f\u4e00\u79cd\u4f4e\u7ea7\u8868\u793a\u5f62\u5f0f\uff0c\u5b83\u662f Python \u6e90\u4ee3\u7801\u7f16\u8bd1\u540e\u7684\u4e2d\u95f4\u5f62\u5f0f\u3002\u5b57\u8282\u7801\u662f\u7531 Python \u865a\u62df\u673a\uff08PVM\uff09\u6267\u884c\u7684\uff0c\u8fd9\u4f7f\u5f97 Python \u7a0b\u5e8f\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u5e73\u53f0\u4e0a\u8fd0\u884c\uff0c\u800c\u4e0d\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b57\u8282\u7801\u7684\u6267\u884c\u8fc7\u7a0b<\/h4>\n<\/p>\n<p><p>Python \u6e90\u4ee3\u7801\u9996\u5148\u88ab\u89e3\u6790\u5668\u89e3\u6790\u4e3a\u62bd\u8c61\u8bed\u6cd5\u6811\uff08AST\uff09\uff0c\u7136\u540e\u7f16\u8bd1\u5668\u5c06 AST \u8f6c\u6362\u4e3a\u5b57\u8282\u7801\u3002\u6700\u540e\uff0cPython \u865a\u62df\u673a\uff08PVM\uff09\u6267\u884c\u8fd9\u4e9b\u5b57\u8282\u7801\u6307\u4ee4\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u67e5\u770b\u5b57\u8282\u7801<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>dis<\/code> \u6a21\u5757\u6765\u67e5\u770b Python \u6e90\u4ee3\u7801\u7684\u5b57\u8282\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import dis<\/p>\n<p>def example_function():<\/p>\n<p>    return sum(range(10))<\/p>\n<p>dis.dis(example_function)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa <code>example_function<\/code> \u7684\u5b57\u8282\u7801\u6307\u4ee4\u3002\u4e86\u89e3\u5b57\u8282\u7801\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3 Python \u7a0b\u5e8f\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u5e76\u8fdb\u884c\u6027\u80fd\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5b57\u8282\u7801\u4f18\u5316\u6280\u672f<\/h3>\n<\/p>\n<p><h4>1\u3001\u5185\u8054\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5185\u8054\u51fd\u6570\u53ef\u4ee5\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u624b\u52a8\u5c06\u5c0f\u51fd\u6570\u7684\u4ee3\u7801\u5185\u8054\u5230\u8c03\u7528\u5904\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5e38\u91cf\u6298\u53e0<\/h4>\n<\/p>\n<p><p>\u5e38\u91cf\u6298\u53e0\u662f\u7f16\u8bd1\u5668\u4f18\u5316\u7684\u4e00\u79cd\u6280\u672f\uff0c\u5b83\u5c06\u8868\u8fbe\u5f0f\u4e2d\u7684\u5e38\u91cf\u503c\u5728\u7f16\u8bd1\u65f6\u8ba1\u7b97\u51fa\u6765\u3002\u4f8b\u5982\uff0c\u8868\u8fbe\u5f0f <code>3 + 4<\/code> \u5728\u7f16\u8bd1\u65f6\u4f1a\u88ab\u4f18\u5316\u4e3a <code>7<\/code>\uff0c\u4ece\u800c\u51cf\u5c11\u8fd0\u884c\u65f6\u7684\u8ba1\u7b97\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5faa\u73af\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u5faa\u73af\u4f18\u5316\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6027\u80fd\u4f18\u5316\u6280\u672f\uff0c\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11\u5faa\u73af\u4f53\u5185\u7684\u8ba1\u7b97\u91cf\u6216\u51cf\u5c11\u5faa\u73af\u7684\u8fed\u4ee3\u6b21\u6570\u6765\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u4e0d\u53d8\u7684\u8ba1\u7b97\u79fb\u51fa\u5faa\u73af\u4f53\uff0c\u6216\u8005\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528 .pyc \u6587\u4ef6\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b9a\u671f\u6e05\u7406\u548c\u91cd\u65b0\u7f16\u8bd1<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6e90\u4ee3\u7801\u53ef\u80fd\u4f1a\u9891\u7e41\u4fee\u6539\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4 <code>.pyc<\/code> \u6587\u4ef6\u8fc7\u671f\u6216\u4e0d\u4e00\u81f4\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u5b9a\u671f\u6e05\u7406\u548c\u91cd\u65b0\u7f16\u8bd1 <code>.pyc<\/code> \u6587\u4ef6\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u4e0e\u6e90\u4ee3\u7801\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u81ea\u52a8\u5316\u7f16\u8bd1\u8fc7\u7a0b<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u6301\u7eed\u96c6\u6210\uff08CI\uff09\u5de5\u5177\uff08\u5982 Jenkins\u3001GitLab CI\uff09\u6765\u81ea\u52a8\u5316\u7f16\u8bd1 <code>.pyc<\/code> \u6587\u4ef6\u7684\u8fc7\u7a0b\u3002\u8fd9\u53ef\u4ee5\u786e\u4fdd\u6bcf\u6b21\u4ee3\u7801\u66f4\u65b0\u540e\uff0c<code>.pyc<\/code> \u6587\u4ef6\u90fd\u80fd\u81ea\u52a8\u751f\u6210\uff0c\u5e76\u4e14\u4e0e\u6e90\u4ee3\u7801\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u76d1\u63a7\u548c\u4f18\u5316\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6027\u80fd\u76d1\u63a7\u5de5\u5177\uff08\u5982 New Relic\u3001Datadog\uff09\u6765\u76d1\u63a7 Python \u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u6839\u636e\u76d1\u63a7\u7ed3\u679c\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u5b57\u8282\u7801\u548c <code>.pyc<\/code> \u6587\u4ef6\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06 Python \u6e90\u4ee3\u7801\u8f6c\u6362\u4e3a <code>.pyc<\/code> \u6587\u4ef6\u662f\u63d0\u9ad8\u7a0b\u5e8f\u6267\u884c\u6548\u7387\u3001\u4fdd\u62a4\u6e90\u4ee3\u7801\u548c\u51cf\u5c11\u89e3\u6790\u65f6\u95f4\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u901a\u8fc7\u4f7f\u7528 <code>compileall<\/code> \u6a21\u5757\u3001<code>py_compile<\/code> \u6a21\u5757\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5de5\u5177\u5982 <code>Cython<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u751f\u6210 <code>.pyc<\/code> \u6587\u4ef6\u3002\u6b64\u5916\uff0c\u4e86\u89e3 <code>.pyc<\/code> \u6587\u4ef6\u7684\u7ed3\u6784\u548c\u5b57\u8282\u7801\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u4f18\u5316 Python \u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5efa\u8bae\u5b9a\u671f\u6e05\u7406\u548c\u91cd\u65b0\u7f16\u8bd1 <code>.pyc<\/code> \u6587\u4ef6\uff0c\u81ea\u52a8\u5316\u7f16\u8bd1\u8fc7\u7a0b\uff0c\u5e76\u76d1\u63a7\u548c\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u3002\u901a\u8fc7\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u786e\u4fdd Python \u5e94\u7528\u7a0b\u5e8f\u5728\u5404\u79cd\u73af\u5883\u4e0b\u90fd\u80fd\u9ad8\u6548\u3001\u7a33\u5b9a\u5730\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06.py\u6587\u4ef6\u8f6c\u6362\u4e3a.pyc\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>py_compile<\/code>\u6a21\u5757\u5c06.py\u6587\u4ef6\u7f16\u8bd1\u4e3a.pyc\u6587\u4ef6\u3002\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a  <\/p>\n<pre><code class=\"language-python\">python -m py_compile your_script.py\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a__pycache__\u76ee\u5f55\uff0c\u5176\u4e2d\u5305\u542b\u7f16\u8bd1\u540e\u7684.pyc\u6587\u4ef6\u3002\u786e\u4fdd\u4f7f\u7528\u4e0e\u539f\u6587\u4ef6\u76f8\u540c\u7684Python\u7248\u672c\uff0c\u4ee5\u907f\u514d\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<p><strong>\u8f6c\u6362\u4e3a.pyc\u6587\u4ef6\u540e\uff0c\u662f\u5426\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c.pyc\u6587\u4ef6\uff1f<\/strong><br \/>\u662f\u7684\uff0c.pyc\u6587\u4ef6\u53ef\u4ee5\u76f4\u63a5\u7531Python\u89e3\u91ca\u5668\u8fd0\u884c\u3002\u53ea\u9700\u4f7f\u7528\u547d\u4ee4\u884c\u8f93\u5165\uff1a  <\/p>\n<pre><code class=\"language-python\">python your_script.pyc\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c\u786e\u4fdd\u5728\u76f8\u540c\u7684\u73af\u5883\u4e2d\u8fd0\u884c.pyc\u6587\u4ef6\uff0c\u4ee5\u907f\u514d\u7531\u4e8e\u7248\u672c\u5dee\u5f02\u5f15\u8d77\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u8981\u5c06.py\u6587\u4ef6\u8f6c\u6362\u4e3a.pyc\u6587\u4ef6\uff1f<\/strong><br \/>\u5c06.py\u6587\u4ef6\u8f6c\u6362\u4e3a.pyc\u6587\u4ef6\u6709\u51e0\u4e2a\u597d\u5904\u3002\u9996\u5148\uff0c.pyc\u6587\u4ef6\u53ef\u4ee5\u52a0\u5feb\u7a0b\u5e8f\u542f\u52a8\u901f\u5ea6\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u5df2\u7f16\u8bd1\u7684\u5b57\u8282\u7801\uff0cPython\u89e3\u91ca\u5668\u4e0d\u9700\u8981\u5728\u6bcf\u6b21\u8fd0\u884c\u65f6\u91cd\u65b0\u7f16\u8bd1\u6e90\u4ee3\u7801\u3002\u5176\u6b21\uff0c.pyc\u6587\u4ef6\u53ef\u4ee5\u5e2e\u52a9\u4fdd\u62a4\u6e90\u4ee3\u7801\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u6613\u4e8e\u4eba\u7c7b\u9605\u8bfb\u3002\u867d\u7136\u8fd9\u5e76\u4e0d\u662f\u5b8c\u5168\u7684\u4fdd\u62a4\uff0c\u4f46\u53ef\u4ee5\u589e\u52a0\u4e00\u4e9b\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8f6c\u6362\u4e3apyc\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u7684 compileall \u6a21\u5757\u3001\u4f7f\u7528 py_compile \u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1169435,"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\/1169423"}],"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=1169423"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169423\/revisions"}],"predecessor-version":[{"id":1169438,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169423\/revisions\/1169438"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1169435"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1169423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1169423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1169423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}