{"id":2851,"date":"2021-04-08T22:25:00","date_gmt":"2021-04-08T14:25:00","guid":{"rendered":"https:\/\/www.yumefx.com\/?p=2851"},"modified":"2022-11-02T11:47:55","modified_gmt":"2022-11-02T03:47:55","slug":"python%e5%ad%97%e8%8a%82%e7%a0%81%e6%96%87%e4%bb%b6pyc","status":"publish","type":"post","link":"https:\/\/www.yumefx.com\/?p=2851","title":{"rendered":"Python\u5b57\u8282\u7801\u6587\u4ef6pyc"},"content":{"rendered":"\n<h1 style=\"text-align: center;\">\u4ec0\u4e48\u662fPython\u5b57\u8282\u7801\u6587\u4ef6<\/h1>\n<p>Python\u662f\u4e00\u79cd\u89e3\u91ca\u6027\u8bed\u8a00\uff0c\u5728Python\u6267\u884cpy\u811a\u672c\u6587\u4ef6\u65f6\uff0cPython\u89e3\u91ca\u5668\u5c06\u6e90\u7801\u8f6c\u6362\u4e3a\u5b57\u8282\u7801\u5199\u5165\u5230\u5185\u5b58\uff0c\u7136\u540e\u518d\u7531\u89e3\u91ca\u5668\u6765\u6267\u884c\u8fd9\u4e9b\u5b57\u8282\u7801\u3002<\/p>\n<p>\u800c\u5728\u4f7f\u7528import\u5bfc\u5165\u5176\u4ed6\u6a21\u5757\u65f6\uff0c\u5219\u4f1a\u5728\u6a21\u5757\u7684py\u6587\u4ef6\u65c1\u8fb9\u81ea\u52a8\u751f\u6210.pyc\u683c\u5f0f\u7684\u540c\u540d\u6587\u4ef6\uff0c\u8fd9\u5c31\u662f\u8be5\u6a21\u5757\u7ecf\u8fc7\u7f16\u8bd1\u540e\u751f\u6210\u7684\u4e8c\u8fdb\u5236\u5b57\u8282\u7801\u6587\u4ef6\uff0c\u4f18\u70b9\u662f\u52a0\u8f7d\u901f\u5ea6\u6709\u6240\u63d0\u5347\u3001\u5b9e\u73b0\u6e90\u7801\u9690\u85cf\u3001\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u9632\u6b62\u53cd\u7f16\u8bd1\u3002<\/p>\n<p>\u6bcf\u6b21\u5728\u5bfc\u5165\u8be5\u6a21\u5757\u65f6\uff0c\u4f1a\u81ea\u52a8\u68c0\u67e5\u5176\u5b57\u8282\u7801\u6587\u4ef6\u7684\u4fee\u6539\u65f6\u95f4\u662f\u5426\u4e0e\u81ea\u8eab\u4e00\u81f4\uff0c\u5982\u679c\u4e00\u81f4\u5219\u76f4\u63a5\u4ece\u8be5\u5b57\u8282\u7801\u6587\u4ef6\u8bfb\u53d6\u5185\u5bb9\uff0c\u5426\u5219\u8be5\u6a21\u5757\u91cd\u65b0\u5bfc\u5165\uff0c\u5e76\u66f4\u65b0\u5f53\u524d\u5df2\u6709\u7684\u5b57\u8282\u7801\u6587\u4ef6\uff0c\u8fd9\u6837\u4fee\u6539\u6a21\u5757\u6e90\u4ee3\u7801\u540e\uff0c\u5b9e\u9645\u4f7f\u7528\u65f6\u4e5f\u4f1a\u81ea\u52a8\u66f4\u65b0\u3002<\/p>\n<p>\u9664\u4e86import\u4ee5\u5916\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7py_compile\u8fd9\u4e2aPython\u81ea\u5e26\u6a21\u5757\uff0c\u5b9e\u73b0\u6279\u91cf\u751f\u6210.pyc\u6587\u4ef6\u3002<\/p>\n\n\n\n<pre><span>import py_compile<\/span>\n<span>py_compile.compile(r\"\/path\/to\/pyfile or pydir\")<\/span>\n<\/pre>\n\n\n\n<h1>&nbsp;<\/h1>\n<h1 style=\"text-align: center;\">\u751f\u6210\u9519\u8bef<\/h1>\n<p>\u6700\u8fd1\u5728\u7ef4\u62a4\u4e00\u4efdPython2.7\u7248\u672c\u7684\u4ee3\u7801\u65f6\uff0c\u53d1\u73b0\u4e00\u76f4\u5e73\u7a33\u8fd0\u884c\u7684\u751f\u4ea7\u73af\u5883\u62a5\u9519\u5bfc\u5165\u7b2c\u4e09\u65b9\u6a21\u5757\u9519\u8befimport error\uff0c\u7ecf\u8fc7\u68c0\u67e5\u53d1\u73b0\u90e8\u5206\u81ea\u52a8\u751f\u6210\u7684.pyc\u5b57\u8282\u7801\u6587\u4ef6\u5927\u5c0f\u53ea\u6709100+\u5b57\u8282\uff0c\u6b63\u5e38\u60c5\u51b5\u4e0b\u5e94\u8be5\u662f\u7565\u5927\u4e8e\u539f\u59cb.py\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n<p>\u5728\u5220\u9664\u8be5.pyc\u6587\u4ef6\u91cd\u65b0\u8fd0\u884c\u540e\uff0c\u81ea\u52a8\u751f\u6210\u7684\u65b0.pyc\u6587\u4ef6\u6b63\u5e38\uff0c\u751f\u4ea7\u73af\u5883\u5f97\u4ee5\u5e73\u7a33\u8fd0\u884c\u3002<\/p>\n<p>\u5728\u641c\u5bfb\u8d44\u6599\u540e\u5f97\u77e5\uff0c\u5728Python\u901a\u8fc7import\u5bfc\u5165py\u6587\u4ef6\u65f6\uff0c\u5982\u679c\u9047\u5230I\/O\u5f02\u5e38\uff0c\u4f1a\u5f97\u5230\u4e00\u4e2aEOF\u6807\u5fd7\uff0c\u7136\u540e\u4ee5\u6240\u8bfb\u53d6\u5230\u7684\u5185\u5bb9\u751f\u6210pyc\u6587\u4ef6\uff0c\u4f46\u8fd9\u4e2a\u8fc7\u7a0b\u5e76\u6ca1\u6709\u68c0\u67e5I\/O\u5f02\u5e38\u3002<\/p>\n<p>\u5728\u6a21\u5757\u540c\u65f6\u8bbf\u95ee\u91cf\u592a\u591a\u3001\u670d\u52a1\u5668\u8d44\u6e90\u7d27\u5f20\u3001\u5e26\u5bbd\u8dd1\u6ee1\u7b49\u60c5\u51b5\u4e0b\u4f1a\u6bd4\u8f83\u5bb9\u6613\u51fa\u73b0\u8fd9\u4e2a\u95ee\u9898\uff0c\u800c\u5728Python3.X\u7248\u672c\u4e2d\u4fee\u590d\u4e86\u6b64bug\uff0c\u56e0\u6b64\u5728Python2.X\u4e2d\u9700\u8981\u591a\u6ce8\u610f\u4e00\u70b9\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6cd5\u662f\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u7981\u6b62\u751f\u6210.pyc\u5b57\u8282\u7801\u6587\u4ef6\uff0c\u867d\u7136\u52a0\u8f7d\u901f\u5ea6\u4f1a\u7a0d\u53d7\u5f71\u54cd\uff0c\u4f46\u8003\u8651\u5230\u7a33\u5b9a\u6027\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002<\/p>\n<h3>&nbsp;<\/h3>\n<h3>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h3>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u662f\u8f83\u4e3a\u5e38\u7528\u7684\uff0c\u4e5f\u6bd4\u8f83\u65b9\u4fbf\u3002<\/p>\n\n\n\n<pre><span>export PYTHONDONTWRITEBYTECODE=1<\/span>\n<\/pre>\n\n\n\n<h3>&nbsp;<\/h3>\n<h3>\u4f7f\u7528-B\u53c2\u6570<\/h3>\n<p>\u5728\u8fd0\u884cpy\u811a\u672c\u65f6\u52a0\u4e0a-B\u53c2\u6570<\/p>\n\n\n\n<pre><span>python -B test.py<\/span>\n<\/pre>\n\n\n\n<h3>&nbsp;<\/h3>\n<h3>\u4f7f\u7528dont_write_bytecode\u51fd\u6570<\/h3>\n<p>\u5728\u5bfc\u5165\u6a21\u5757\u524d\u5199\u5165\u4ee5\u4e0b\u4ee3\u7801<\/p>\n\n\n\n<pre><span>import sys<\/span>\n<span>sys.dont_write_bytecode = True<\/span>\n<\/pre>\n\n\n\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">\u53cd\u7f16\u8bd1<\/h1>\n<p>\u524d\u9762\u8bf4\u8fc7pyc\u5b57\u8282\u7801\u6587\u4ef6\u53ea\u80fd\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u9632\u53cd\u7f16\u8bd1\uff0c\u5176\u5b9e\u53ea\u80fd\u9632\u4e00\u4e0b\u5c0f\u767d\uff0c\u901a\u8fc7uncompyle6\u8fd9\u4e2a\u7b2c\u4e09\u65b9\u5e93\u5c31\u53ef\u4ee5\u628apyc\u5b57\u8282\u7801\u6587\u4ef6\u5b8c\u7f8e\u53cd\u7f16\u8bd1\u6210py\u6e90\u6587\u4ef6\uff0c\u8fde\u6ce8\u91ca\u90fd\u80fd\u4fdd\u7559\u3002<\/p>\n<p>\u9996\u5148\u5b89\u88c5uncompyle6\u3002<\/p>\n\n\n\n<pre>\n<span>pip install uncompyle6<\/span>\n<\/pre>\n\n\n\n<p>\u7136\u540e\u5728CMD\u7a97\u53e3\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4<\/p>\n\n\n\n<pre>\n<span>uncompyle6 -o test.py test.pyc<\/span>\n<\/pre>\n\n\n\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06test.pyc\u5b57\u8282\u7801\u6587\u4ef6\u53cd\u7f16\u8bd1\u6210test.py\u6e90\u7801\u6587\u4ef6\u4e86\uff0c\u5982\u679c\u62a5\u9519\u627e\u4e0d\u5230uncompyle6\uff0c\u5c31\u628apython\u5b89\u88c5\u76ee\u5f55\u4e0b\u7684Scripts\u6587\u4ef6\u5939\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684PATH\u73af\u5883\u53d8\u91cf\u91cc\u3002<\/p>\n\n\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e5e\u4e10\u4e0d\u4f1a\u5992\u5fcc\u767e\u4e07\u5bcc\u7fc1\uff0c<br \/>\u4f46\u4ed6\u80af\u5b9a\u4f1a\u5992\u5fcc\u6536\u5165\u66f4\u9ad8\u7684\u4e5e\u4e10\u3002<\/p>\n<p style=\"text-align: right;\">\u2014\u2014\u7f57\u7d20<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662fPython\u5b57\u8282\u7801\u6587\u4ef6 Python\u662f\u4e00\u79cd\u89e3\u91ca\u6027\u8bed\u8a00\uff0c\u5728Python\u6267\u884cpy\u811a\u672c\u6587\u4ef6\u65f6\uff0cPython\u89e3\u91ca [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2852,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,4],"tags":[37,14],"_links":{"self":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts\/2851"}],"collection":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2851"}],"version-history":[{"count":0,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts\/2851\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/media\/2852"}],"wp:attachment":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}