{"id":204511,"date":"2025-05-29T13:06:29","date_gmt":"2025-05-29T05:06:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204511\/"},"modified":"2025-05-29T13:06:29","modified_gmt":"2025-05-29T05:06:29","slug":"%e4%bd%bf%e7%94%a8-pyinstaller-%e6%89%93%e5%8c%85%e6%97%b6%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5%e8%87%aa%e5%ae%9a%e4%b9%89%e6%a8%a1%e5%9d%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204511\/","title":{"rendered":"\u4f7f\u7528 Pyinstaller \u6253\u5305\u65f6\u5982\u4f55\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528 Pyinstaller \u6253\u5305\u65f6\u5982\u4f55\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\uff1f<\/h1>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u4f7f\u7528 Pyinstaller \u6253\u5305\u65f6\u5982\u4f55\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241109\/1731137986672f11c2560d1.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u7528 pyinstaller \u6253\u5305\u65f6\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 pyinstaller \u5c06 python \u6587\u4ef6\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\u540e\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u5bfc\u5165\u81ea\u5df1\u7f16\u5199\u7684\u6a21\u5757\u3002\u4ee5\u4e0b\u662f\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u68c0\u67e5\u6a21\u5757\u7684\u4f9d\u8d56\u5173\u7cfb\uff1a<\/strong>\u786e\u4fdd\u60a8\u7684\u6a21\u5757\u6ca1\u6709\u4f9d\u8d56\u4e8e\u5c1a\u672a\u5b89\u88c5\u5728\u76ee\u6807\u8ba1\u7b97\u673a\u4e0a\u7684\u5176\u4ed6\u6a21\u5757\u3002<\/li>\n<li><strong>\u4f7f\u7528 &#8211;hidden-import \u9009\u9879\uff1a<\/strong>\u5728 pyinstaller \u547d\u4ee4\u4e2d\u4f7f\u7528 &#8211;hidden-import \u9009\u9879\u53ef\u4ee5\u6307\u5b9a\u8981\u5bfc\u5165\u7684\u81ea\u5b9a\u4e49\u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>pyinstaller calltest.py --onefile --hidden-import sample_test<\/pre>\n<ol>\n<li><strong>\u6253\u5305\u81ea\u5b9a\u4e49\u6a21\u5757\uff1a<\/strong>\u5982\u679c\u60a8\u9700\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u6a21\u5757\u4e2d\u5305\u542b\u7684\u8d44\u6e90\uff0c\u5219\u8fd8\u5fc5\u987b\u5c06\u8be5\u6a21\u5757\u6253\u5305\u4e3a\u6570\u636e\u6587\u4ef6\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 &#8211;add-data \u9009\u9879\u6765\u5b9e\u73b0\u6b64\u76ee\u7684\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>pyinstaller sample_test.py --onefile --hidden-import sample_test --add-data .\/temp\/sample_test:.\/<\/pre>\n<p>\u6b64\u547d\u4ee4\u5c06\u6a21\u5757\u53ca\u5176\u8d44\u6e90\u6253\u5305\u5230\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u3002<\/p>\n<ol>\n<li><strong>\u8bbe\u7f6e pythonpath \u73af\u5883\u53d8\u91cf\uff1a<\/strong>\u5728\u5bfc\u5165\u4e86\u81ea\u5b9a\u4e49\u6a21\u5757\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u8fd0\u884c\u65f6\uff0c\u9700\u8981\u8bbe\u7f6e pythonpath \u73af\u5883\u53d8\u91cf\u4ee5\u6307\u5411\u6a21\u5757\u7684\u4f4d\u7f6e\u3002\u53ef\u4ee5\u4f7f\u7528 os.add_dll_directory \u51fd\u6570\u6765\u5b9e\u73b0\u6b64\u76ee\u7684\u3002<\/li>\n<\/ol>\n<pre>import os\nos.add_dll_directory('.\/TEMP')<\/pre>\n<p>\u6267\u884c\u8fd9\u4e9b\u6b65\u9aa4\u540e\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u5728\u6ca1\u6709\u5b89\u88c5 python \u7684\u8ba1\u7b97\u673a\u4e0a\u6210\u529f\u5bfc\u5165\u548c\u4f7f\u7528\u81ea\u5df1\u7684\u6a21\u5757\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u4f7f\u7528 Pyinstaller \u6253\u5305\u65f6\u5982\u4f55\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Pyinstaller \u6253&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204511","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204511"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204511\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}