{"id":1119305,"date":"2025-01-08T18:43:30","date_gmt":"2025-01-08T10:43:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1119305.html"},"modified":"2025-01-08T18:43:32","modified_gmt":"2025-01-08T10:43:32","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%86%99%e4%b8%80%e4%b8%aa%e5%ae%89%e8%a3%85%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1119305.html","title":{"rendered":"\u5982\u4f55\u7528python\u5199\u4e00\u4e2a\u5b89\u88c5\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082444\/96247aea-fc64-4a46-89e3-b0c59eb36dca.webp\" alt=\"\u5982\u4f55\u7528python\u5199\u4e00\u4e2a\u5b89\u88c5\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u5199\u4e00\u4e2a\u5b89\u88c5\u7a0b\u5e8f<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u5199\u4e00\u4e2a\u5b89\u88c5\u7a0b\u5e8f\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u8bbe\u8ba1\u548c\u5b9a\u4e49\u5b89\u88c5\u6d41\u7a0b\u3001\u4f7f\u7528\u5408\u9002\u7684\u5b89\u88c5\u5305\u521b\u5efa\u5de5\u5177\u3001\u7f16\u5199\u811a\u672c\u5b9e\u73b0\u5b89\u88c5\u903b\u8f91\u3001\u6d4b\u8bd5\u548c\u8c03\u8bd5\u5b89\u88c5\u7a0b\u5e8f<\/strong>\u3002\u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5b89\u88c5\u5305\u521b\u5efa\u5de5\u5177\u975e\u5e38\u91cd\u8981\uff0c\u5e38\u89c1\u7684\u5de5\u5177\u5305\u62ecPyInstaller\u3001cx_Freeze\u7b49\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PyInstaller\u6765\u521b\u5efa\u4e00\u4e2a\u5b89\u88c5\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u8bbe\u8ba1\u548c\u5b9a\u4e49\u5b89\u88c5\u6d41\u7a0b<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199\u5b89\u88c5\u7a0b\u5e8f\u4e4b\u524d\uff0c\u9996\u5148\u8981\u660e\u786e\u5b89\u88c5\u7a0b\u5e8f\u9700\u8981\u5b8c\u6210\u7684\u4efb\u52a1\u548c\u6d41\u7a0b\u3002\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u68c0\u67e5\u7cfb\u7edf\u73af\u5883\u548c\u4f9d\u8d56\u9879\u3002<\/li>\n<li>\u590d\u5236\u5e94\u7528\u7a0b\u5e8f\u6587\u4ef6\u5230\u76ee\u6807\u76ee\u5f55\u3002<\/li>\n<li>\u914d\u7f6e\u5e94\u7528\u7a0b\u5e8f\u7684\u8fd0\u884c\u73af\u5883\u3002<\/li>\n<li>\u521b\u5efa\u684c\u9762\u5feb\u6377\u65b9\u5f0f\u6216\u542f\u52a8\u811a\u672c\u3002<\/li>\n<li>\u63d0\u793a\u7528\u6237\u5b89\u88c5\u5b8c\u6210\u3002<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u9009\u62e9\u5408\u9002\u7684\u5b89\u88c5\u5305\u521b\u5efa\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7f16\u5199\u5b89\u88c5\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5de5\u5177\uff0c\u8fd9\u91cc\u63a8\u8350\u4f7f\u7528PyInstaller\u3002PyInstaller\u53ef\u4ee5\u5c06Python\u811a\u672c\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u9002\u7528\u4e8eWindows\u3001Mac\u548cLinux\u5e73\u53f0\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528PyInstaller\u521b\u5efa\u5b89\u88c5\u7a0b\u5e8f<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5PyInstaller<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5PyInstaller\u3002\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u7f16\u5199Python\u811a\u672c<\/li>\n<\/ol>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/code>\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import sys<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;Hello, this is a simple installation program.&quot;)<\/p>\n<p>    input(&quot;Press Enter to exit...&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528PyInstaller\u6253\u5305<\/li>\n<\/ol>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5c06<code>main.py<\/code>\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u547d\u4ee4\u4f1a\u5728<code>dist<\/code>\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a\u540d\u4e3a<code>main<\/code>\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff08\u5728Windows\u4e0a\u4e3a<code>main.exe<\/code>\uff09\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7f16\u5199\u5b89\u88c5\u811a\u672c<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u521b\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684\u5b89\u88c5\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u811a\u672c\u6765\u81ea\u52a8\u6267\u884c\u5b89\u88c5\u6b65\u9aa4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u5b89\u88c5\u811a\u672c<code>install.py<\/code>\uff0c\u7528\u4e8e\u5c06\u5e94\u7528\u7a0b\u5e8f\u6587\u4ef6\u590d\u5236\u5230\u76ee\u6807\u76ee\u5f55\u5e76\u521b\u5efa\u684c\u9762\u5feb\u6377\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>import sys<\/p>\n<p>import platform<\/p>\n<p>def create_shortcut(target, shortcut_path, description):<\/p>\n<p>    if platform.system() == &quot;Windows&quot;:<\/p>\n<p>        import winshell<\/p>\n<p>        with winshell.shortcut(shortcut_path) as shortcut:<\/p>\n<p>            shortcut.path = target<\/p>\n<p>            shortcut.description = description<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Shortcut creation is only supported on Windows.&quot;)<\/p>\n<p>def install():<\/p>\n<p>    target_dir = os.path.join(os.getenv(&quot;APPDATA&quot;), &quot;MyApp&quot;)<\/p>\n<p>    if not os.path.exists(target_dir):<\/p>\n<p>        os.makedirs(target_dir)<\/p>\n<p>    src_file = os.path.join(os.path.dirname(__file__), &quot;dist&quot;, &quot;main.exe&quot;)<\/p>\n<p>    dst_file = os.path.join(target_dir, &quot;main.exe&quot;)<\/p>\n<p>    shutil.copy(src_file, dst_file)<\/p>\n<p>    shortcut_path = os.path.join(os.path.join(os.getenv(&quot;USERPROFILE&quot;), &quot;Desktop&quot;), &quot;MyApp.lnk&quot;)<\/p>\n<p>    create_shortcut(dst_file, shortcut_path, &quot;MyApp Shortcut&quot;)<\/p>\n<p>    print(&quot;Installation complete.&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    install()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u6d4b\u8bd5\u548c\u8c03\u8bd5\u5b89\u88c5\u7a0b\u5e8f<\/p>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u5b89\u88c5\u811a\u672c\u540e\uff0c\u9700\u8981\u8fdb\u884c\u6d4b\u8bd5\u548c\u8c03\u8bd5\uff0c\u786e\u4fdd\u5b89\u88c5\u7a0b\u5e8f\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\u3002\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e0b\u8fdb\u884c\u6d4b\u8bd5\uff0c\u68c0\u67e5\u662f\u5426\u6240\u6709\u6587\u4ef6\u90fd\u6b63\u786e\u590d\u5236\uff0c\u5feb\u6377\u65b9\u5f0f\u662f\u5426\u521b\u5efa\u6210\u529f\uff0c\u7a0b\u5e8f\u662f\u5426\u80fd\u591f\u6b63\u5e38\u542f\u52a8\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u6253\u5305\u5b89\u88c5\u7a0b\u5e8f<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fbf\u4e8e\u5206\u53d1\uff0c\u53ef\u4ee5\u5c06\u6574\u4e2a\u5b89\u88c5\u7a0b\u5e8f\u6253\u5305\u6210\u4e00\u4e2a\u538b\u7f29\u6587\u4ef6\u6216\u8005\u521b\u5efa\u4e00\u4e2a\u5b89\u88c5\u5305\u3002\u53ef\u4ee5\u4f7f\u7528Inno Setup\uff08Windows\uff09\u6216\u5176\u4ed6\u5b89\u88c5\u5305\u521b\u5efa\u5de5\u5177\u6765\u751f\u6210\u5b89\u88c5\u5305\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u5199\u4e00\u4e2a\u5b89\u88c5\u7a0b\u5e8f\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u8bbe\u8ba1\u548c\u5b9a\u4e49\u5b89\u88c5\u6d41\u7a0b\u3001\u4f7f\u7528\u5408\u9002\u7684\u5b89\u88c5\u5305\u521b\u5efa\u5de5\u5177\u3001\u7f16\u5199\u811a\u672c\u5b9e\u73b0\u5b89\u88c5\u903b\u8f91\u3001\u6d4b\u8bd5\u548c\u8c03\u8bd5\u5b89\u88c5\u7a0b\u5e8f<\/strong>\u3002\u901a\u8fc7\u4f7f\u7528PyInstaller\u548c\u7f16\u5199\u5b89\u88c5\u811a\u672c\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u4e00\u4e2a\u53ef\u5206\u53d1\u7684\u5b89\u88c5\u7a0b\u5e8f\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u4e3a\u60a8\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u53c2\u8003\uff0c\u5e2e\u52a9\u60a8\u6210\u529f\u521b\u5efaPython\u5b89\u88c5\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u521b\u5efa\u4e00\u4e2a\u53ef\u6267\u884c\u7684\u5b89\u88c5\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8981\u4f7f\u7528Python\u521b\u5efa\u4e00\u4e2a\u5b89\u88c5\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u50cfPyInstaller\u6216cx_Freeze\u8fd9\u6837\u7684\u5de5\u5177\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5c06Python\u811a\u672c\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u7528\u6237\u65e0\u9700\u5b89\u88c5Python\u73af\u5883\u3002\u4f60\u9700\u8981\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff0c\u7136\u540e\u901a\u8fc7\u7b80\u5355\u7684\u547d\u4ee4\u884c\u6307\u4ee4\u751f\u6210\u5b89\u88c5\u5305\u3002\u786e\u4fdd\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\u5c06\u6240\u6709\u4f9d\u8d56\u9879\u548c\u8d44\u6e90\u6587\u4ef6\u5305\u542b\u5728\u5185\uff0c\u4ee5\u4fbf\u7528\u6237\u53ef\u4ee5\u987a\u5229\u8fd0\u884c\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u5728\u521b\u5efa\u5b89\u88c5\u7a0b\u5e8f\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u5b89\u88c5\u7a0b\u5e8f\u65f6\uff0c\u786e\u4fdd\u8003\u8651\u7528\u6237\u7684\u64cd\u4f5c\u7cfb\u7edf\u548c\u73af\u5883\u3002\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u53ef\u80fd\u9700\u8981\u4e0d\u540c\u7684\u6253\u5305\u65b9\u5f0f\uff0c\u4f8b\u5982Windows\u548cLinux\u7684\u53ef\u6267\u884c\u6587\u4ef6\u683c\u5f0f\u4e0d\u540c\u3002\u6b64\u5916\uff0c\u63d0\u4f9b\u6e05\u6670\u7684\u5b89\u88c5\u8bf4\u660e\u548c\u7cfb\u7edf\u8981\u6c42\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5b89\u88c5\u548c\u8fd0\u884c\u4f60\u7684\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u4f9d\u8d56\u5173\u7cfb\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u5b89\u88c5\u7a0b\u5e8f\u65f6\uff0c\u5904\u7406\u4f9d\u8d56\u5173\u7cfb\u975e\u5e38\u91cd\u8981\u3002\u4f7f\u7528\u5de5\u5177\u5982pip\u548crequirements.txt\u6587\u4ef6\u6765\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u5305\u3002\u5728\u6253\u5305\u65f6\uff0c\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u9879\u90fd\u88ab\u5305\u62ec\u5728\u5185\uff0c\u6216\u8005\u63d0\u4f9b\u4e00\u4e2a\u6e05\u6670\u7684\u6587\u6863\uff0c\u6307\u5bfc\u7528\u6237\u5982\u4f55\u5b89\u88c5\u8fd9\u4e9b\u4f9d\u8d56\u3002\u8fd9\u5c06\u786e\u4fdd\u7528\u6237\u5728\u8fd0\u884c\u4f60\u7684\u7a0b\u5e8f\u65f6\u4e0d\u4f1a\u9047\u5230\u7f3a\u5931\u5e93\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u5199\u4e00\u4e2a\u5b89\u88c5\u7a0b\u5e8f \u4f7f\u7528Python\u5199\u4e00\u4e2a\u5b89\u88c5\u7a0b\u5e8f\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u8bbe\u8ba1\u548c\u5b9a\u4e49\u5b89\u88c5\u6d41\u7a0b\u3001\u4f7f\u7528\u5408\u9002\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1119313,"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\/1119305"}],"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=1119305"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1119305\/revisions"}],"predecessor-version":[{"id":1119315,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1119305\/revisions\/1119315"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1119313"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1119305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1119305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1119305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}