{"id":1120952,"date":"2025-01-08T19:03:52","date_gmt":"2025-01-08T11:03:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1120952.html"},"modified":"2025-01-08T19:03:54","modified_gmt":"2025-01-08T11:03:54","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e6%b2%a1%e6%9c%89%e7%8e%af%e5%a2%83%e7%9a%84%e7%94%b5%e8%84%91%e4%b8%8a%e6%89%a7%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1120952.html","title":{"rendered":"python\u5982\u4f55\u5728\u6ca1\u6709\u73af\u5883\u7684\u7535\u8111\u4e0a\u6267\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083417\/af7c8c5c-ddef-4b26-9d8f-1dec706f134e.webp\" alt=\"python\u5982\u4f55\u5728\u6ca1\u6709\u73af\u5883\u7684\u7535\u8111\u4e0a\u6267\u884c\" \/><\/p>\n<p><p> <strong>\u5728\u6ca1\u6709Python\u73af\u5883\u7684\u7535\u8111\u4e0a\u6267\u884cPython\u811a\u672c\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528PyInstaller\u3001\u4f7f\u7528Py2exe\u3001\u4f7f\u7528Docker\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u3001\u4f7f\u7528\u5728\u7ebfIDE\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528PyInstaller\u662f\u6bd4\u8f83\u5e38\u89c1\u4e14\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002PyInstaller\u80fd\u591f\u5c06Python\u811a\u672c\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\uff0c\u8fd9\u6837\u5373\u4f7f\u76ee\u6807\u7535\u8111\u4e0a\u6ca1\u6709\u5b89\u88c5Python\u73af\u5883\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u8fd9\u4e9b\u53ef\u6267\u884c\u6587\u4ef6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PyInstaller\u6765\u6253\u5305Python\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PyInstaller<\/h3>\n<\/p>\n<p><p><strong>PyInstaller<\/strong> \u662f\u4e00\u4e2a\u5e38\u7528\u7684Python\u6253\u5305\u5de5\u5177\uff0c\u5b83\u80fd\u591f\u5c06Python\u811a\u672c\u53ca\u5176\u4f9d\u8d56\u9879\u6253\u5305\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u5177\u4f53\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5PyInstaller<\/h4>\n<\/p>\n<p><p>\u5728\u4f60\u7684\u5f00\u53d1\u73af\u5883\u4e2d\u5b89\u88c5PyInstaller\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6253\u5305Python\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u7684Python\u811a\u672c\u540d\u4e3a<code>script.py<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c06\u5176\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff08\u5728Windows\u7cfb\u7edf\u4e2d\u662f<code>.exe<\/code>\u6587\u4ef6\uff09\u3002<code>--onefile<\/code>\u9009\u9879\u8868\u793a\u5c06\u6240\u6709\u4f9d\u8d56\u9879\u6253\u5305\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u66f4\u4fbf\u4e8e\u5206\u53d1\u3002<\/p>\n<\/p>\n<p><h4>\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u76ee\u6807\u7535\u8111\u4e0a\u627e\u5230\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u53cc\u51fb\u5373\u53ef\u8fd0\u884c\uff0c\u65e0\u9700Python\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Py2exe<\/h3>\n<\/p>\n<p><p><strong>Py2exe<\/strong> \u662f\u53e6\u4e00\u4e2a\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3aWindows\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Py2exe<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u5b89\u88c5Py2exe\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install py2exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efasetup\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>setup.py<\/code>\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from distutils.core import setup<\/p>\n<p>import py2exe<\/p>\n<p>setup(console=[&#39;script.py&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8fd0\u884csetup\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884csetup\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py py2exe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u751f\u6210\u4e00\u4e2a<code>dist<\/code>\u6587\u4ef6\u5939\uff0c\u5176\u4e2d\u5305\u542b\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Docker<\/h3>\n<\/p>\n<p><p><strong>Docker<\/strong> \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u865a\u62df\u5316\u6280\u672f\uff0c\u53ef\u4ee5\u5c06\u5e94\u7528\u7a0b\u5e8f\u53ca\u5176\u8fd0\u884c\u73af\u5883\u6253\u5305\u6210\u4e00\u4e2a\u5bb9\u5668\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aDocker\u955c\u50cf\uff0c\u7136\u540e\u5728\u76ee\u6807\u7535\u8111\u4e0a\u8fd0\u884cDocker\u5bb9\u5668\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efaDockerfile<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>Dockerfile<\/code>\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-Dockerfile\">FROM python:3.8-slim<\/p>\n<p>COPY . \/app<\/p>\n<p>WORKDIR \/app<\/p>\n<p>RUN pip install -r requirements.txt<\/p>\n<p>CMD [&quot;python&quot;, &quot;script.py&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6784\u5efaDocker\u955c\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6784\u5efaDocker\u955c\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">docker build -t my-python-app .<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8fd0\u884cDocker\u5bb9\u5668<\/h4>\n<\/p>\n<p><p>\u5728\u76ee\u6807\u7535\u8111\u4e0a\u8fd0\u884cDocker\u5bb9\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">docker run my-python-app<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p><strong>\u865a\u62df\u73af\u5883<\/strong> \u53ef\u4ee5\u5c06Python\u53ca\u5176\u4f9d\u8d56\u9879\u6253\u5305\u5728\u4e00\u8d77\uff0c\u7136\u540e\u5728\u76ee\u6807\u7535\u8111\u4e0a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b89\u88c5\u4f9d\u8d56\u9879<\/h4>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u5e76\u5b89\u88c5\u4f9d\u8d56\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source myenv\/bin\/activate  # For Unix or MacOS<\/p>\n<p>myenv\\Scripts\\activate  # For Windows<\/p>\n<p>pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6253\u5305\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982<code>zip<\/code>\u6216<code>tar<\/code>\u5c06\u865a\u62df\u73af\u5883\u6253\u5305\uff0c\u7136\u540e\u5728\u76ee\u6807\u7535\u8111\u4e0a\u89e3\u538b\u5e76\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5728\u7ebfIDE<\/h3>\n<\/p>\n<p><p><strong>\u5728\u7ebfIDE<\/strong> \u5982Google Colab\u3001Repl.it\u7b49\uff0c\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u8fd0\u884cPython\u811a\u672c\uff0c\u65e0\u9700\u5728\u672c\u5730\u7535\u8111\u4e0a\u5b89\u88c5Python\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528Google Colab<\/h4>\n<\/p>\n<ol>\n<li>\u6253\u5f00Google Colab\uff08<a href=\"https:\/\/colab.research.google.com\/%EF%BC%89\">https:\/\/colab.research.google.com\/\uff09<\/a><\/li>\n<li>\u65b0\u5efa\u4e00\u4e2aNotebook<\/li>\n<li>\u5728Notebook\u4e2d\u7f16\u5199\u5e76\u8fd0\u884cPython\u4ee3\u7801<\/li>\n<\/ol>\n<p><h4>\u4f7f\u7528Repl.it<\/h4>\n<\/p>\n<ol>\n<li>\u6253\u5f00Repl.it\uff08<a href=\"https:\/\/repl.it\/%EF%BC%89\">https:\/\/repl.it\/\uff09<\/a><\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Repl<\/li>\n<li>\u5728\u7f16\u8f91\u5668\u4e2d\u7f16\u5199\u5e76\u8fd0\u884cPython\u4ee3\u7801<\/li>\n<\/ol>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u6ca1\u6709Python\u73af\u5883\u7684\u7535\u8111\u4e0a\u6267\u884cPython\u811a\u672c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5176\u4e2d<strong>\u4f7f\u7528PyInstaller<\/strong> \u662f\u6700\u5e38\u89c1\u4e14\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528PyInstaller\uff0c\u4f60\u53ef\u4ee5\u5c06Python\u811a\u672c\u53ca\u5176\u4f9d\u8d56\u9879\u6253\u5305\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u76f4\u63a5\u5728\u76ee\u6807\u7535\u8111\u4e0a\u8fd0\u884c\u800c\u65e0\u9700\u5b89\u88c5Python\u73af\u5883\u3002\u5176\u4ed6\u65b9\u6cd5\u5982<strong>Py2exe\u3001Docker\u3001\u865a\u62df\u73af\u5883\u3001\u5728\u7ebfIDE<\/strong> \u4e5f\u5404\u6709\u4f18\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728\u6ca1\u6709Python\u73af\u5883\u7684\u7535\u8111\u4e0a\uff0c\u5982\u4f55\u8fd0\u884cPython\u4ee3\u7801\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5728\u7ebfPython\u7f16\u8bd1\u5668\u548cIDE\uff0c\u4f8b\u5982Repl.it\u3001Google Colab\u6216\u8005Jupyter Notebook\u3002\u53ea\u9700\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u8fd9\u4e9b\u5de5\u5177\uff0c\u5c06\u4f60\u7684\u4ee3\u7801\u7c98\u8d34\u8fdb\u53bb\u5e76\u6267\u884c\uff0c\u5c31\u80fd\u770b\u5230\u4ee3\u7801\u7684\u8fd0\u884c\u7ed3\u679c\uff0c\u65e0\u9700\u5728\u672c\u5730\u5b89\u88c5\u4efb\u4f55Python\u73af\u5883\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u65b9\u6cd5\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982PyInstaller\u6216cx_Freeze\u5c06Python\u4ee3\u7801\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u5728\u6ca1\u6709Python\u73af\u5883\u7684\u7535\u8111\u4e0a\u8fd0\u884c\u8be5\u6587\u4ef6\uff0c\u65e0\u9700\u5b89\u88c5\u4efb\u4f55\u989d\u5916\u7684\u8f6f\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u6ca1\u6709\u5b89\u88c5Python\u7684\u60c5\u51b5\u4e0b\u8fdb\u884cPython\u5b66\u4e60\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5728\u7ebf\u5b66\u4e60\u5e73\u53f0\u548c\u6559\u7a0b\u8fdb\u884c\u5b66\u4e60\uff0c\u8fd9\u4e9b\u5e73\u53f0\u63d0\u4f9b\u4e86\u4ea4\u4e92\u5f0f\u7f16\u7a0b\u73af\u5883\uff0c\u4f8b\u5982Codecademy\u3001Coursera\u6216edX\u3002\u8bb8\u591a\u5e73\u53f0\u5141\u8bb8\u7528\u6237\u5728\u5b66\u4e60\u8fc7\u7a0b\u4e2d\u76f4\u63a5\u8fd0\u884c\u4ee3\u7801\uff0c\u5e2e\u52a9\u7406\u89e3Python\u7684\u57fa\u672c\u6982\u5ff5\u4e0e\u8bed\u6cd5\uff0c\u800c\u65e0\u9700\u4efb\u4f55\u672c\u5730\u73af\u5883\u8bbe\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u6ca1\u6709Python\u73af\u5883\u7684\u7535\u8111\u4e0a\u6267\u884cPython\u811a\u672c\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528PyInstaller\u3001\u4f7f\u7528Py2exe\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1120960,"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\/1120952"}],"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=1120952"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120952\/revisions"}],"predecessor-version":[{"id":1120966,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120952\/revisions\/1120966"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1120960"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1120952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1120952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1120952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}