{"id":1122454,"date":"2025-01-08T19:20:47","date_gmt":"2025-01-08T11:20:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1122454.html"},"modified":"2025-01-08T19:20:51","modified_gmt":"2025-01-08T11:20:51","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%9b%be%e7%89%87%e6%89%93%e5%8c%85%e8%bf%9bexe%e9%87%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1122454.html","title":{"rendered":"python\u5982\u4f55\u5c06\u56fe\u7247\u6253\u5305\u8fdbexe\u91cc"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084315\/98879bf9-fa73-4c6a-a8dd-b8846a0107d7.webp\" alt=\"python\u5982\u4f55\u5c06\u56fe\u7247\u6253\u5305\u8fdbexe\u91cc\" \/><\/p>\n<p><p> <strong>\u8981\u5c06\u56fe\u7247\u6253\u5305\u8fdbPython\u751f\u6210\u7684exe\u6587\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528PyInstaller\u3001Pillow\u7b49\u5de5\u5177\u3002\u901a\u8fc7PyInstaller\uff0c\u4f60\u53ef\u4ee5\u5c06Python\u811a\u672c\u53ca\u5176\u4f9d\u8d56\u9879\uff08\u5305\u62ec\u56fe\u7247\u6587\u4ef6\uff09\u6253\u5305\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8be6\u7ec6\u6b65\u9aa4\u5305\u62ec<\/strong>\uff1a\u5c06\u56fe\u7247\u6587\u4ef6\u653e\u5165\u9879\u76ee\u76ee\u5f55\u3001\u5728.spec\u6587\u4ef6\u4e2d\u6dfb\u52a0\u6570\u636e\u6587\u4ef6\u8def\u5f84\u3001\u4f7f\u7528PyInstaller\u6253\u5305\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u4e2a\u6b65\u9aa4\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<\/h2>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u8f6f\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u548cPyInstaller\u3002\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5PyInstaller\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u9879\u76ee\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u6253\u5305\u4e4b\u524d\uff0c\u5efa\u8bae\u5c06\u9879\u76ee\u6587\u4ef6\u6574\u7406\u6e05\u6670\u3002\u5047\u8bbe\u4f60\u7684\u9879\u76ee\u76ee\u5f55\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>my_project\/<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>\u251c\u2500\u2500 images\/<\/p>\n<p>\u2502   \u2514\u2500\u2500 example.png<\/p>\n<p>\u2514\u2500\u2500 other_files\/<\/p>\n<p>    \u2514\u2500\u2500 config.json<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>main.py<\/code>\u662f\u4f60\u7684\u4e3bPython\u811a\u672c\uff0c<code>images\/<\/code>\u76ee\u5f55\u4e0b\u5305\u542b\u9700\u8981\u6253\u5305\u7684\u56fe\u7247\u6587\u4ef6\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e8c\u3001\u7f16\u5199Python\u811a\u672c<\/h2>\n<\/p>\n<p><h3>1\u3001\u5f15\u7528\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u6765\u5904\u7406\u56fe\u7247\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Pillow\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u4f60\u7684<code>main.py<\/code>\u4e2d\u5f15\u7528\u56fe\u7247\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>def show_image():<\/p>\n<p>    image_path = &#39;images\/example.png&#39;<\/p>\n<p>    img = Image.open(image_path)<\/p>\n<p>    img.show()<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    show_image()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u6d4b\u8bd5\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5728\u6253\u5305\u4e4b\u524d\uff0c\u5148\u5728Python\u73af\u5883\u4e2d\u8fd0\u884c\u811a\u672c\uff0c\u786e\u4fdd\u6ca1\u6709\u9519\u8bef\u5e76\u4e14\u56fe\u7247\u53ef\u4ee5\u6b63\u5e38\u663e\u793a\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e09\u3001\u521b\u5efa.spec\u6587\u4ef6<\/h2>\n<\/p>\n<p><h3>1\u3001\u751f\u6210\u521d\u59cb.spec\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528PyInstaller\u751f\u6210\u4e00\u4e2a\u521d\u59cb\u7684.spec\u6587\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u5c06\u7528\u4e8e\u914d\u7f6e\u6253\u5305\u8fc7\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pyinstaller --onefile --name=my_project main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6761\u547d\u4ee4\u5c06\u751f\u6210\u4e00\u4e2a\u540d\u4e3a<code>my_project.spec<\/code>\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7f16\u8f91.spec\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6253\u5f00\u751f\u6210\u7684.spec\u6587\u4ef6\uff0c\u627e\u5230<code>a = Analysis(...)<\/code>\u884c\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u6570\u636e\u6587\u4ef6\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = Analysis([&#39;main.py&#39;],<\/p>\n<p>             pathex=[&#39;\/path\/to\/my_project&#39;],<\/p>\n<p>             binaries=[],<\/p>\n<p>             datas=[(&#39;images\/example.png&#39;, &#39;images&#39;)],<\/p>\n<p>             hiddenimports=[],<\/p>\n<p>             hookspath=[],<\/p>\n<p>             runtime_hooks=[],<\/p>\n<p>             excludes=[],<\/p>\n<p>             win_no_prefer_redirects=False,<\/p>\n<p>             win_private_assemblies=False,<\/p>\n<p>             cipher=block_cipher,<\/p>\n<p>             noarchive=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>datas<\/code>\u5217\u8868\u4e2d\uff0c\u6dfb\u52a0\u4e86\u56fe\u7247\u6587\u4ef6\u8def\u5f84\uff0c\u8868\u793a\u8981\u5c06\u8be5\u56fe\u7247\u6587\u4ef6\u6253\u5305\u8fdbexe\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u66f4\u65b0EXE\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u5728.spec\u6587\u4ef6\u4e2d\u627e\u5230<code>exe = EXE(...)<\/code>\u90e8\u5206\uff0c\u5c06<code>EXE<\/code>\u5bf9\u8c61\u7684<code>name<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a\u4f60\u60f3\u8981\u7684\u53ef\u6267\u884c\u6587\u4ef6\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">exe = EXE(pyz,<\/p>\n<p>          a.scripts,<\/p>\n<p>          [],<\/p>\n<p>          exclude_binaries=True,<\/p>\n<p>          name=&#39;my_project&#39;,<\/p>\n<p>          debug=False,<\/p>\n<p>          bootloader_ignore_signals=False,<\/p>\n<p>          strip=False,<\/p>\n<p>          upx=True,<\/p>\n<p>          console=True )<\/p>\n<p><\/code><\/pre>\n<\/p>\n<hr>\n<p><h2>\u56db\u3001\u6253\u5305\u9879\u76ee<\/h2>\n<\/p>\n<p><h3>1\u3001\u8fd0\u884cPyInstaller<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4fee\u6539\u540e\u7684.spec\u6587\u4ef6\uff0c\u901a\u8fc7PyInstaller\u6253\u5305\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pyinstaller my_project.spec<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u68c0\u67e5\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u6253\u5305\u5b8c\u6210\u540e\uff0c\u4f60\u4f1a\u5728<code>dist\/<\/code>\u76ee\u5f55\u4e0b\u627e\u5230\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8fd0\u884c\u8fd9\u4e2aexe\u6587\u4ef6\uff0c\u786e\u4fdd\u56fe\u7247\u80fd\u591f\u6b63\u786e\u663e\u793a\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u529e\u6cd5<\/h2>\n<\/p>\n<p><h3>1\u3001\u8def\u5f84\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u6253\u5305\u6210exe\u540e\uff0c\u6587\u4ef6\u8def\u5f84\u53ef\u80fd\u4f1a\u6709\u6240\u53d8\u5316\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u83b7\u53d6\u76f8\u5bf9\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import sys<\/p>\n<p>def resource_path(relative_path):<\/p>\n<p>    &quot;&quot;&quot; Get absolute path to resource, works for dev and for PyInstaller &quot;&quot;&quot;<\/p>\n<p>    base_path = getattr(sys, &#39;_MEIPASS&#39;, os.path.dirname(os.path.abspath(__file__)))<\/p>\n<p>    return os.path.join(base_path, relative_path)<\/p>\n<p>image_path = resource_path(&#39;images\/example.png&#39;)<\/p>\n<p>img = Image.open(image_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u6253\u5305\u5931\u8d25<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6253\u5305\u8fc7\u7a0b\u4e2d\u9047\u5230\u9519\u8bef\uff0c\u68c0\u67e5.spec\u6587\u4ef6\u4e2d\u7684\u8def\u5f84\u662f\u5426\u6b63\u786e\uff0c\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u9879\u5df2\u7ecf\u5b89\u88c5\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p><strong>\u901a\u8fc7PyInstaller\u548cPillow\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5c06\u56fe\u7247\u6587\u4ef6\u6253\u5305\u8fdbPython\u751f\u6210\u7684exe\u6587\u4ef6\u4e2d<\/strong>\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4ece\u51c6\u5907\u5de5\u4f5c\u3001\u7f16\u5199Python\u811a\u672c\u3001\u521b\u5efa\u548c\u7f16\u8f91.spec\u6587\u4ef6\u5230\u6700\u7ec8\u6253\u5305\u9879\u76ee\u7684\u5b8c\u6574\u6b65\u9aa4\uff0c\u5e76\u63d0\u4f9b\u4e86\u793a\u4f8b\u4ee3\u7801\u548c\u5e38\u89c1\u95ee\u9898\u7684\u89e3\u51b3\u529e\u6cd5\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u786e\u4fdd\u751f\u6210\u7684exe\u6587\u4ef6\u80fd\u591f\u6b63\u786e\u5f15\u7528\u548c\u663e\u793a\u56fe\u7247\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u56fe\u7247\u5d4c\u5165\u5230EXE\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u5c06\u56fe\u7247\u6253\u5305\u8fdbEXE\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528PyInstaller\u7b49\u5de5\u5177\u3002\u901a\u8fc7\u5728\u6253\u5305\u65f6\u6307\u5b9a\u56fe\u7247\u8def\u5f84\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u6267\u884cEXE\u65f6\u7a0b\u5e8f\u80fd\u591f\u8bbf\u95ee\u5230\u8fd9\u4e9b\u8d44\u6e90\u3002\u901a\u5e38\uff0c\u60a8\u9700\u8981\u5728.spec\u6587\u4ef6\u4e2d\u6dfb\u52a0\u76f8\u5173\u7684\u8d44\u6e90\u8def\u5f84\uff0c\u5e76\u5728\u6253\u5305\u547d\u4ee4\u4e2d\u5f15\u7528\u8be5.spec\u6587\u4ef6\u3002<\/p>\n<p><strong>\u6253\u5305\u540e\u7684EXE\u6587\u4ef6\u80fd\u5426\u5728\u6ca1\u6709Python\u73af\u5883\u7684\u673a\u5668\u4e0a\u8fd0\u884c\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u901a\u8fc7\u4f7f\u7528PyInstaller\u7b49\u5de5\u5177\u6253\u5305\u7684EXE\u6587\u4ef6\u53ef\u4ee5\u5728\u6ca1\u6709Python\u73af\u5883\u7684\u673a\u5668\u4e0a\u8fd0\u884c\u3002\u8fd9\u79cd\u6253\u5305\u65b9\u5f0f\u4f1a\u5c06Python\u89e3\u91ca\u5668\u53ca\u6240\u9700\u7684\u4f9d\u8d56\u9879\u4e00\u5e76\u6253\u5305\uff0c\u786e\u4fdd\u7528\u6237\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u7a0b\u5e8f\u800c\u65e0\u9700\u5b89\u88c5Python\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u6253\u5305\u7684\u56fe\u7247\u5728\u8fd0\u884c\u65f6\u80fd\u591f\u6b63\u5e38\u663e\u793a\uff1f<\/strong><br \/>\u4e3a\u786e\u4fdd\u6253\u5305\u7684\u56fe\u7247\u5728\u8fd0\u884c\u65f6\u6b63\u5e38\u663e\u793a\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u5f15\u7528\u56fe\u7247\u6587\u4ef6\uff0c\u800c\u4e0d\u662f\u7edd\u5bf9\u8def\u5f84\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u56e0\u4e0d\u540c\u8ba1\u7b97\u673a\u7684\u6587\u4ef6\u7cfb\u7edf\u7ed3\u6784\u4e0d\u540c\u800c\u5bfc\u81f4\u7684\u95ee\u9898\u3002\u6b64\u5916\uff0c\u5728\u6253\u5305\u65f6\u4e00\u5b9a\u8981\u5728.spec\u6587\u4ef6\u4e2d\u6b63\u786e\u6dfb\u52a0\u8d44\u6e90\u6587\u4ef6\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u5305\u542b\u5728EXE\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06\u56fe\u7247\u6253\u5305\u8fdbPython\u751f\u6210\u7684exe\u6587\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528PyInstaller\u3001Pillow\u7b49\u5de5\u5177\u3002\u901a\u8fc7PyI [&hellip;]","protected":false},"author":3,"featured_media":1122461,"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\/1122454"}],"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=1122454"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122454\/revisions"}],"predecessor-version":[{"id":1122462,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122454\/revisions\/1122462"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1122461"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1122454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1122454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1122454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}