{"id":1658,"date":"2024-12-10T14:35:08","date_gmt":"2024-12-10T06:35:08","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=1658"},"modified":"2024-12-10T14:38:30","modified_gmt":"2024-12-10T06:38:30","slug":"%e6%89%93%e5%8c%85%e4%bf%9d%e6%8a%a4%e4%bd%a0%e7%9a%84-python-%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=1658","title":{"rendered":"\u6253\u5305\u4fdd\u62a4\u4f60\u7684 Python \u7a0b\u5e8f"},"content":{"rendered":"<h2>1. \u7f16\u8bd1\u6210\u53ef\u6267\u884c\u6587\u4ef6<\/h2>\n<p>\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5c06 Python \u811a\u672c\u8f6c\u6362\u4e3a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/p>\n<ul>\n<li>\u5de5\u5177\uff1a<\/li>\n<li>PyInstaller: \u652f\u6301\u591a\u5e73\u53f0\uff08Windows\u3001macOS \u548c Linux\uff09\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-shell\">pyinstaller --onefile your_script.py<\/code><\/pre>\n<p>\u4e0a\u8ff0\u547d\u4ee4\u4f1a\u751f\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4f4d\u4e8e dist\/ \u76ee\u5f55\u4e0b\u3002<\/p>\n<ul>\n<li>\n<p>cx_Freeze: \u4e5f\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u5de5\u5177\uff0c\u652f\u6301\u7c7b\u4f3c\u529f\u80fd\u3002<\/p>\n<pre><code class=\"language-shell\">cxfreeze your_script.py --target-dir dist\/<\/code><\/pre>\n<\/li>\n<li>\n<p>py2exe\uff08Windows \u4e13\u7528\uff09: \u5c06 Python \u811a\u672c\u7f16\u8bd1\u6210 Windows \u4e0b\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li>\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e0d\u9700\u8981 Python \u73af\u5883\u5373\u53ef\u8fd0\u884c\u3002<\/li>\n<li>\u90e8\u5206\u60c5\u51b5\u4e0b\u53ef\u4ee5\u6df7\u6dc6\u6216\u9690\u85cf\u6e90\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7f3a\u70b9\uff1a<\/p>\n<ul>\n<li>\u7528\u4e13\u4e1a\u7684\u53cd\u7f16\u8bd1\u5de5\u5177\u53ef\u80fd\u4ecd\u7136\u8fd8\u539f\u90e8\u5206\u4ee3\u7801\u903b\u8f91\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>2. \u5b57\u8282\u7801\u4fdd\u62a4<\/h2>\n<p>Python \u6e90\u4ee3\u7801\u5728\u8fd0\u884c\u65f6\u4f1a\u88ab\u7f16\u8bd1\u6210\u5b57\u8282\u7801\u6587\u4ef6\uff08.pyc\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u751f\u6210 .pyc \u6587\u4ef6\u5206\u53d1\u4ee3\u7801\uff1a<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528 compileall \u6a21\u5757\u751f\u6210\u5b57\u8282\u7801\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-shell\">python -m compileall your_script.py<\/code><\/pre>\n<p>\u7136\u540e\u5206\u53d1\u751f\u6210\u7684 .pyc \u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u914d\u5408\u5de5\u5177\u5982 cython \u6216 Pyarmor\uff0c\u53ef\u4ee5\u52a0\u5bc6\u6216\u8fdb\u4e00\u6b65\u4fdd\u62a4\u5b57\u8282\u7801\u6587\u4ef6\u3002<\/p>\n<\/li>\n<\/ul>\n<h2>3. \u4f7f\u7528 Cython \u5c06 Python \u8f6c\u6362\u4e3a C \u4ee3\u7801<\/h2>\n<p>\u4f7f\u7528 Cython \u5c06 Python \u811a\u672c\u8f6c\u6362\u4e3a C \u4ee3\u7801\uff0c\u5e76\u7f16\u8bd1\u4e3a\u5171\u4eab\u5e93\u6216\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/p>\n<ul>\n<li>\n<p>\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5 Cython\uff1a<\/p>\n<pre><code class=\"language-shell\">pip install cython<\/code><\/pre>\n<\/li>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a setup.py \u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-python\">\nfrom setuptools import setup\nfrom Cython.Build import cythonize<\/code><\/pre>\n<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p>setup(<br \/>\next_modules=cythonize(&quot;your_script.py&quot;),<br \/>\n)<\/p>\n<pre><code>\n 3. \u7f16\u8bd1\uff1a\n```shell\npython setup.py build_ext --inplace\n<\/code><\/pre>\n<ol start=\"4\">\n<li>\n<p>\u751f\u6210 .so \u6216 .pyd \u6587\u4ef6\u540e\uff0c\u5206\u53d1\u800c\u4e0d\u662f\u6e90\u4ee3\u7801\u3002<\/p>\n<ul>\n<li>\u4f18\u70b9\uff1a\n<ul>\n<li>\u6027\u80fd\u53ef\u80fd\u4f1a\u63d0\u5347\u3002<\/li>\n<li>\u751f\u6210\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u96be\u4ee5\u9006\u5411\u8fd8\u539f\u6e90\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>4. \u52a0\u5bc6\u4e0e\u8fd0\u884c\u65f6\u89e3\u5bc6<\/h2>\n<p>\u4f7f\u7528\u52a0\u5bc6\u5de5\u5177\u5c06\u4ee3\u7801\u52a0\u5bc6\uff0c\u7136\u540e\u8fd0\u884c\u65f6\u52a8\u6001\u89e3\u5bc6\uff1a<\/p>\n<ul>\n<li>\u5de5\u5177\uff1a<\/li>\n<li>Pyarmor: \u5bf9 Python \u811a\u672c\u8fdb\u884c\u52a0\u5bc6\u5e76\u9650\u5236\u8fd0\u884c\u73af\u5883\uff08\u4f8b\u5982\uff0c\u7ed1\u5b9a\u5230\u6307\u5b9a\u8bbe\u5907\u6216\u65f6\u95f4\uff09\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-shell\">pyarmor pack -x &quot; --onefile&quot; your_script.py<\/code><\/pre>\n<ul>\n<li>\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\u52a0\u5bc6-\u89e3\u5bc6\u903b\u8f91\uff1a\u4f46\u8fd0\u884c\u65f6\u89e3\u5bc6\u7684\u90e8\u5206\u53ef\u80fd\u88ab\u9006\u5411\u5de5\u7a0b\u3002<\/li>\n<\/ul>\n<h2>5. Docker \u955c\u50cf\u5c01\u88c5<\/h2>\n<p>\u5c06 Python \u811a\u672c\u5c01\u88c5\u5230 Docker \u955c\u50cf\u4e2d\uff0c\u7528\u6237\u53ea\u8fd0\u884c\u5bb9\u5668\uff0c\u800c\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u4ee3\u7801\uff1a<\/p>\n<ul>\n<li>\u521b\u5efa Dockerfile\uff0c\u5c06\u811a\u672c\u62f7\u8d1d\u5230\u955c\u50cf\u5185\u3002<\/li>\n<li>\u5206\u53d1 Docker \u955c\u50cf\uff0c\u800c\u975e\u6e90\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<h2>6. \u4ee3\u7801\u6df7\u6dc6<\/h2>\n<p>\u4f7f\u7528\u4ee3\u7801\u6df7\u6dc6\u5de5\u5177\u5c06\u4ee3\u7801\u7ed3\u6784\u548c\u53d8\u91cf\u540d\u6df7\u6dc6\uff1a<\/p>\n<ul>\n<li>\u5de5\u5177\uff1a\n<ul>\n<li>pyminifier: \u4ee3\u7801\u538b\u7f29\u548c\u6df7\u6dc6\u5de5\u5177\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"language-shell\">pyminifier --obfuscate your_script.py<\/code><\/pre>\n<ul>\n<li>\u6548\u679c\uff1a\n<ul>\n<li>\u589e\u52a0\u9605\u8bfb\u96be\u5ea6\uff0c\u4f46\u5bf9\u9ad8\u7ea7\u653b\u51fb\u8005\u53ef\u80fd\u4f5c\u7528\u6709\u9650\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<ul>\n<li>\u5982\u679c\u4f60\u9700\u8981\u7b80\u5355\u4fdd\u62a4\uff0cPyInstaller \u6216 Cython \u662f\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u66f4\u9ad8\u7684\u5b89\u5168\u6027\uff0c\u53ef\u4ee5\u7ed3\u5408 Cython \u548c Pyarmor\u3002<\/li>\n<li>\u90e8\u7f72\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 Docker \u6765\u5c01\u88c5\u73af\u5883\uff0c\u4ece\u800c\u907f\u514d\u76f4\u63a5\u66b4\u9732\u4ee3\u7801\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. \u7f16\u8bd1\u6210\u53ef\u6267\u884c\u6587\u4ef6 \u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5c06 Python \u811a\u672c\u8f6c\u6362\u4e3a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff1a \u5de5\u5177\uff1a PyInstaller: \u652f\u6301\u591a\u5e73\u53f0\uff08Windows\u3001macOS \u548c Linux\uff09\u3002 pyinstaller &#8211;onefile your_script.py \u4e0a\u8ff0\u547d\u4ee4\u4f1a\u751f\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4f4d\u4e8e dist\/ \u76ee\u5f55\u4e0b\u3002 cx_Freeze: \u4e5f\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u5de5\u5177\uff0c\u652f\u6301\u7c7b\u4f3c\u529f\u80fd\u3002 cxfreeze your_script.py &#8211;target-dir dist\/ py2exe\uff08Windows \u4e13\u7528\uff09: \u5c06 Python \u811a\u672c\u7f16\u8bd1\u6210 Windows \u4e0b\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002 \u4f18\u70b9\uff1a \u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e0d\u9700\u8981 Python \u73af\u5883\u5373\u53ef\u8fd0\u884c\u3002 \u90e8\u5206\u60c5\u51b5\u4e0b\u53ef\u4ee5\u6df7\u6dc6\u6216\u9690\u85cf\u6e90\u4ee3\u7801\u3002 \u7f3a\u70b9\uff1a \u7528\u4e13\u4e1a\u7684\u53cd\u7f16\u8bd1\u5de5\u5177\u53ef\u80fd\u4ecd\u7136\u8fd8\u539f\u90e8\u5206\u4ee3\u7801\u903b\u8f91\u3002 2. \u5b57\u8282\u7801\u4fdd\u62a4 Python \u6e90\u4ee3\u7801\u5728\u8fd0\u884c\u65f6\u4f1a\u88ab\u7f16\u8bd1\u6210\u5b57\u8282\u7801\u6587\u4ef6\uff08.pyc\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u751f\u6210 .pyc \u6587\u4ef6\u5206\u53d1\u4ee3\u7801\uff1a \u4f7f\u7528 compileall \u6a21\u5757\u751f\u6210\u5b57\u8282\u7801\u6587\u4ef6\uff1a python -m compileall your_script.py \u7136\u540e\u5206\u53d1\u751f\u6210\u7684 .pyc \u6587\u4ef6\u3002 \u914d\u5408\u5de5\u5177\u5982 cython \u6216 [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=1658\" title=\"Permanent Link to: \u6253\u5305\u4fdd\u62a4\u4f60\u7684 Python \u7a0b\u5e8f\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1658","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1658"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1658"}],"version-history":[{"count":4,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1658\/revisions"}],"predecessor-version":[{"id":1662,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1658\/revisions\/1662"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}