{"id":85,"date":"2025-06-30T18:01:17","date_gmt":"2025-06-30T10:01:17","guid":{"rendered":"https:\/\/www.idcyz.com\/blog\/?p=85"},"modified":"2025-06-30T18:01:17","modified_gmt":"2025-06-30T10:01:17","slug":"%e5%b0%86python%e8%84%9a%e6%9c%ac%e5%bf%ab%e9%80%9f%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%8f%af%e6%89%a7%e8%a1%8c%e6%96%87%e4%bb%b6%e7%9a%84%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.idcyz.com\/blog\/85.html","title":{"rendered":"\u5c06Python\u811a\u672c\u5feb\u901f\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u7684\u6307\u5357"},"content":{"rendered":"<p><p>\u5728\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5c06 Python \u811a\u672c\uff08.py \u6587\u4ef6\uff09\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\uff0c\u4ee5\u4fbf\u4e8e\u5206\u53d1\u548c\u8fd0\u884c\u800c\u65e0\u9700\u7528\u6237\u81ea\u884c\u5b89\u88c5 Python \u73af\u5883\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 PyInstaller \u5de5\u5177\u5c06 Python \u6587\u4ef6\u8f6c\u6362\u4e3a .exe \u683c\u5f0f\uff0c\u64cd\u4f5c\u7b80\u5355\u4e14\u9ad8\u6548\uff0c\u9002\u5408\u9700\u8981\u8de8\u5e73\u53f0\u5206\u53d1\u7684\u5f00\u53d1\u8005\u4f7f\u7528\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.idcyz.com\/blog\/wp-content\/uploads\/2025\/06\/hi0Kk.jpg\" alt=\"\u5c06Python\u811a\u672c\u5feb\u901f\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u7684\u6307\u5357\" title=\"\u5c06Python\u811a\u672c\u5feb\u901f\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u7684\u6307\u5357\"><\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u64cd\u4f5c\u524d\u7684\u51c6\u5907<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u7684\u8ba1\u7b97\u673a\u4e0a\u5df2\u5b89\u88c5\u4ee5\u4e0b\u7ec4\u4ef6\uff1a<\/p>\n<\/p>\n<ul>\n<li>Python\uff08\u5efa\u8bae\u4f7f\u7528\u6700\u65b0\u7248\u672c\uff0c\u63a8\u8350 3.6 \u53ca\u4ee5\u4e0a\uff09<\/li>\n<li>pip\uff08Python \u5305\u7ba1\u7406\u5de5\u5177\uff0c\u901a\u5e38\u968f Python \u81ea\u52a8\u5b89\u88c5\uff09<\/li>\n<li>\u5fc5\u8981\u7684 Python \u5e93\uff08\u5982\u6709\u4f7f\u7528\u5230\uff09<\/li>\n<\/ul>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 <strong>PyInstaller<\/strong> \u5de5\u5177\u5c06 .py \u6587\u4ef6\u8f6c\u6362\u4e3a .exe \u6587\u4ef6\u3002\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e\u8ba4\u60a8\u5df2\u5b89\u88c5 <strong>PyInstaller<\/strong>\uff0c\u5982\u679c\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install pyinstaller<\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528 PyInstaller \u7684\u8be6\u7ec6\u6b65\u9aa4<\/h2>\n<\/p>\n<p><h3>1. \u7f16\u5199 Python \u811a\u672c<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 Python \u811a\u672c\uff0c\u547d\u540d\u4e3a <strong>example.py<\/strong>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>print(\"Hello, World!\")<\/p>\r\n<p>input(\"Press Enter to exit...\")<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6253\u5f00\u547d\u4ee4\u884c\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u5728 Windows \u4e2d\uff0c\u53ef\u4ee5\u6309\u4e0b <strong>Win + R<\/strong> \u7ec4\u5408\u952e\uff0c\u8f93\u5165 <strong>cmd<\/strong> \u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u3002<\/p>\n<\/p>\n<p><h3>3. \u5bfc\u822a\u81f3\u811a\u672c\u6240\u5728\u76ee\u5f55<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <strong>cd<\/strong> \u547d\u4ee4\u5207\u6362\u5230\u60a8\u7684 Python \u811a\u672c\u6240\u5728\u76ee\u5f55\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code>cd C:\\path\\to\\your\\script<\/code><\/pre>\n<\/p>\n<p><h3>4. \u8fd0\u884c PyInstaller<\/h3>\n<\/p>\n<p><p>\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5c06 Python \u811a\u672c\u6253\u5305\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code>pyinstaller --onefile example.py<\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<strong>&#8211;onefile<\/strong> \u53c2\u6570\u8868\u793a\u751f\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>5. \u68c0\u67e5\u751f\u6210\u7684\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6253\u5305\u5b8c\u6210\u540e\uff0c\u60a8\u5c06\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u627e\u5230\u65b0\u7684 <strong>dist<\/strong> \u6587\u4ef6\u5939\uff0c\u5176\u4e2d\u5305\u542b\u751f\u6210\u7684 <strong>example.exe<\/strong> \u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>6. \u6d4b\u8bd5\u53ef\u6267\u884c\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u53cc\u51fb <strong>example.exe<\/strong> \u6587\u4ef6\uff0c\u786e\u8ba4\u5176\u8fd0\u884c\u6b63\u5e38\uff0c\u5e76\u8f93\u51fa <strong>Hello, World!<\/strong>\u3002\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u60a8\u6210\u529f\u5c06 .py \u6587\u4ef6\u8f6c\u6362\u4e3a .exe \u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5e38\u89c1\u914d\u7f6e\u9009\u9879\u548c\u547d\u4ee4<\/h2>\n<\/p>\n<p><p><strong>PyInstaller<\/strong> \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u4ed6\u53c2\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8fdb\u884c\u8c03\u6574\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>&#8211;noconsole<\/strong>\uff1a\u4f7f\u7528\u6b64\u9009\u9879\u53ef\u4ee5\u9690\u85cf\u547d\u4ee4\u884c\u63a7\u5236\u53f0\u7a97\u53e3\uff0c\u4ec5\u663e\u793a\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u3002<\/li>\n<li><strong>&#8211;icon=icon.ico<\/strong>\uff1a\u53ef\u4ee5\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u6dfb\u52a0\u56fe\u6807\u3002\u9700\u8981\u5c06\u56fe\u6807\u6587\u4ef6\u51c6\u5907\u597d\u3002<\/li>\n<li><strong>&#8211;add-data &#8220;data;data&#8221;<\/strong>\uff1a\u5c06\u989d\u5916\u7684\u6570\u636e\u6587\u4ef6\u5305\u542b\u5728\u6253\u5305\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\uff08\u5982\u914d\u7f6e\u6587\u4ef6\u7b49\uff09\u3002\u6839\u636e\u9700\u8981\u8c03\u6574\u8def\u5f84\u548c\u5206\u9694\u7b26\u3002<\/li>\n<\/ul>\n<p><h2>\u56db\u3001\u64cd\u4f5c\u4e2d\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/h2>\n<\/p>\n<p><h3>1. \u4f9d\u8d56\u7f3a\u5931\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5728\u8fd0\u884c .exe \u6587\u4ef6\u65f6\u51fa\u73b0\u4f9d\u8d56\u7f3a\u5931\u7684\u95ee\u9898\uff0c\u53ef\u80fd\u662f\u672a\u5c06\u67d0\u4e9b\u5e93\u6b63\u786e\u5305\u542b\u5728\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u8be6\u7ec6\u68c0\u67e5\u9519\u8bef\u4fe1\u606f\u6216\u4f7f\u7528 <strong>&#8211;hidden-import<\/strong> \u9009\u9879\u6765\u624b\u52a8\u6dfb\u52a0\u7f3a\u5931\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>2. \u9632\u75c5\u6bd2\u8f6f\u4ef6\u8bef\u62a5<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u9632\u75c5\u6bd2\u8f6f\u4ef6\u4f1a\u5c06\u65b0\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u8bef\u62a5\u4e3a\u75c5\u6bd2\u3002\u5c1d\u8bd5\u5c06\u751f\u6210\u7684\u6587\u4ef6\u6dfb\u52a0\u5230\u9632\u75c5\u6bd2\u8f6f\u4ef6\u7684\u767d\u540d\u5355\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>3. \u6587\u4ef6\u8def\u5f84\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u9700\u8981\u8bbf\u95ee\u6587\u4ef6\uff0c\u8bf7\u786e\u4fdd\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6216\u8005\u5904\u7406\u76f8\u5bf9\u8def\u5f84\u7684\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528 <strong>os.path<\/strong> \u5e93\u4e2d\u7684 <strong>join<\/strong> \u65b9\u6cd5\uff0c\u4ee5\u907f\u514d\u8def\u5f84\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3\u4e0e\u5b9e\u7528\u6280\u5de7<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u6210\u529f\u5c06 .py \u6587\u4ef6\u8f6c\u6362\u4e3a .exe \u6587\u4ef6\uff0c\u4ee5\u4fbf\u4e8e\u5206\u53d1\u548c\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u989d\u5916\u7684\u5b9e\u7528\u6280\u5de7\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728\u6253\u5305\u524d\uff0c\u6e05\u7406\u60a8\u7684\u9879\u76ee\u6587\u4ef6\u5939\uff0c\u786e\u4fdd\u6ca1\u6709\u4e0d\u5fc5\u8981\u7684\u6587\u4ef6\u4ee5\u907f\u514d\u6253\u5305\u65f6\u4ea7\u751f\u6df7\u4e71\u3002<\/li>\n<li>\u7ecf\u5e38\u6d4b\u8bd5\u60a8\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u786e\u4fdd\u5176\u5728\u4e0d\u540c\u7684\u673a\u5668\u4e0a\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/li>\n<li>\u4f7f\u7528\u9879\u76ee\u7ba1\u7406\u5de5\u5177\uff08\u5982 Git\uff09\u4fdd\u6301\u4ee3\u7801\u7248\u672c\u7684\u66f4\u65b0\uff0c\u4ee5\u4fbf\u5728\u53d8\u5316\u65f6\u8fdb\u884c\u5feb\u901f\u56de\u6eda\u3002<\/li>\n<\/ul>\n<p><p>\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u60a8\u5feb\u901f\u638c\u63e1\u5c06 Python \u6587\u4ef6\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u7684\u6280\u80fd\uff0c\u63d0\u9ad8\u60a8\u7684\u5f00\u53d1\u6548\u7387\u3002\u82e5\u6709\u5176\u4ed6\u95ee\u9898\uff0c\u8bf7\u968f\u65f6\u67e5\u9605 <strong>PyInstaller<\/strong> \u5b98\u65b9\u6587\u6863\u6216\u5bfb\u6c42\u793e\u533a\u652f\u6301\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5c06 Python \u811a\u672c\uff08.py \u6587\u4ef6\uff09\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\uff0c\u4ee5\u4fbf\u4e8e\u5206\u53d1\u548c\u8fd0\u884c\u800c\u65e0\u9700\u7528\u6237\u81ea\u884c\u5b89\u88c5 Python \u73af\u5883\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 PyInstaller \u5de5\u5177\u5c06 Python \u6587\u4ef6\u8f6c\u6362\u4e3a .exe&#8230;<\/p>\n","protected":false},"author":1,"featured_media":86,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[50,51,12],"topic":[],"class_list":["post-85","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jiushu","tag-exe","tag-pyinstaller","tag-python"],"_links":{"self":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":1,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":88,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/85\/revisions\/88"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/media\/86"}],"wp:attachment":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/tags?post=85"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/topic?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}