{"id":2920,"date":"2026-08-27T20:53:26","date_gmt":"2026-08-27T12:53:26","guid":{"rendered":"https:\/\/thereisno.top\/?p=2920"},"modified":"2026-08-27T20:53:26","modified_gmt":"2026-08-27T12:53:26","slug":"python-%e6%89%93%e5%8c%85exe%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/thereisno.top\/?p=2920","title":{"rendered":"python \u6253\u5305exe\u6587\u4ef6"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u524d\u8a00<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">python \u4ee3\u7801\u7531\u4e8e\u662f\u811a\u672c\u8bed\u8a00\uff0c\u5982\u679c\u662f\u9700\u8981\u4fdd\u5bc6\u7684\u7b97\u6cd5\uff0c\u6700\u597d\u662f\u4f7f\u7528<code>Cython<\/code>\u5c06python\u4ee3\u7801\u7f16\u8bd1\u6210C\u8bed\u8a00\uff0c\u518d\u4f7f\u7528C\u7f16\u8bd1\u5668\u7f16\u8bd1\uff0c\u7f16\u8bd1\u540e\u96be\u4ee5\u9006\u5411\u3002 \u5728Windows\u5e73\u53f0\u4e0a\u9996\u5148\u4f7f\u7528\u5fae\u8f6f\u7684<code>MSVC<\/code>\u7f16\u8bd1\u3002\u8003\u8651\u5fae\u8f6f\u7684<code>MSVC<\/code>\u73af\u5883\u4f53\u79ef\u8f83\u5927\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528gcc\u6216\u8005clang\u8fdb\u884c\u7f16\u8bd1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528MSVC \u7f16\u8bd1\u5668<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5b89\u88c5MSVC \u4e0b\u8f7d<code>vs_BuildTools.exe<\/code>\uff0c\u7136\u540e\u5b89\u88c5\uff0c\u9009\u62e9\u7b2c\u4e00\u4e2a\u201c\u4f7f\u7528C++\u7684\u684c\u9762\u5f00\u53d1\u201d\uff0c\u5b89\u88c5\u8be6\u60c5\u4fdd\u6301\u9ed8\u8ba4\u7684\u52fe\u9009\u9879\uff0c\u5927\u7ea6\u9700\u89816GB\u5de6\u53f3\u7684\u7a7a\u95f4\u3002<\/li>\n\n\n\n<li>\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5728path\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u8def\u5f84<br>C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\&lt;\u7248\u672c\u53f7&gt;\\bin\\Hostx64\\x64<br>C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\&lt;\u7248\u672c\u53f7&gt;\\include <br>C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\&lt;\u7248\u672c\u53f7&gt;\\lib\\x64 <\/code><\/pre>\n\n\n\n<!--more-->\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u7f16\u8bd1<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code><em># setup.py<\/em><br>import os<br>from setuptools import setup<br>from Cython.Build import cythonize<br>from setuptools import Extension<br><br><em># \u5b9a\u4e49\u9700\u8981\u7f16\u8bd1\u7684\u6269\u5c55\u6a21\u5757<\/em><br>extensions = &#91;<br>    Extension(\"license_manager\", &#91;\"license.py\"]),<br>    Extension(\"main_run\", &#91;\"mainrun.py\"])<br>]<br><br>setup(<br>    name=\"my_license_manager\",<br>    version=\"0.0.1\",<br>    ext_modules=cythonize(extensions)<br>)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># \u7f16\u8bd1<br>python setup.py build_ext --inplace<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li>\u4fee\u6539py\u5bfc\u5165\u4e3apyd\u6587\u4ef6<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code><em># license_manager \u548c\u4e0a\u9762\u7684\u6a21\u5757\u540d\u5b57\u4e00\u6837 Extension(\"license_manager\", &#91;\"license.py\"])<\/em><br>from license_manager import LicenseManager <\/code><\/pre>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li>\u6253\u5305<\/li>\n<\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u4f7f\u7528<code>--add-binary<\/code>\u6307\u5b9a\u4e0a\u9762\u7f16\u8bd1\u8f93\u51fa\u7684pyd\u6587\u4ef6\uff0c\u4f7f\u7528<code>--add-data<\/code>\u6307\u5b9a\u8d44\u6e90\u6587\u4ef6\uff0c\u4f7f\u7528<code>--hidden-import<\/code>\u9690\u85cf\u4f9d\u8d56\u5305\uff0c\u9759\u6001\u8d44\u6e90\u653e\u5728res\u6587\u4ef6\u5939\u4e0b\uff0c\u6253\u5305\u5230res\u76ee\u5f55\u4e0b\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u539f\u59cb\u8def\u5f84\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6253\u5305\u540e\u7684\u8def\u5f84\uff0c<code>.<\/code>\u8868\u793a\u6839\u76ee\u5f55\u3002<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code># \u6253\u5305<br>pyinstaller  -F -w --add-data \"res;res\" run.py  --add-binary \"main_run.cp313-win_amd64.pyd;.\"  --add-binary \"license_manager.cp313-win_amd64.pyd;.\" --hidden-import xml --hidden-import xml.etree --hidden-import xml.etree.ElementTree --hidden-import wmi --hidden-import ctypes<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff1a<code>res<\/code>\u76ee\u5f55\u6253\u5305\u5230exe\u540e\uff0c\u5e76\u975e\u771f\u6b63\u4eceexe\u4e2d\u52a0\u8f7d\uff0c\u800c\u662f\u81ea\u52a8\u89e3\u538bexe\u4e2d\u7684\u8d44\u6e90\u6587\u4ef6\u5230<code>_MEIPASS<\/code>\u76ee\u5f55\uff0c\u7136\u540e\u4ece<code>_MEIPASS<\/code>\u76ee\u5f55\u52a0\u8f7d\u8d44\u6e90\u6587\u4ef6\u3002\u6240\u4ee5\u9700\u8981\u6ce8\u610f\u83b7\u53d6\u8d44\u6e90\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>def _get_resource_path(self,relative_path: str) -&gt; str:<br>    \"\"\"\u83b7\u53d6\u8d44\u6e90\u7edd\u5bf9\u8def\u5f84\uff0c\u517c\u5bb9pyinstaller -F \/ -D\"\"\"<br>    if getattr(sys, 'frozen', False):<br>        <em># \u6253\u5305\u540e\u8fd0\u884c<\/em><br>        base_path = sys._MEIPASS<br>    else:<br>        <em># \u5f00\u53d1\u8c03\u8bd5\uff0c\u811a\u672c\u6240\u5728\u76ee\u5f55<\/em><br>        base_path = os.path.abspath(\".\")<br>    return os.path.join(base_path, relative_path)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528clang\/gcc\u7f16\u8bd1<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5b89\u88c5clang\/gcc<\/li>\n<\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5b89\u88c5\u5b8cMSYS2\u540e\uff0c\u627e\u5230<code>\u76f8\u5e94\u7684\u547d\u4ee4\u884c\u7ec8\u7aef<\/code>\uff0c\u5982\u679c\u4f7f\u7528<code>clang<\/code>\u73af\u5883\uff0c\u9700\u8981\u6253\u5f00clang64\u7ec8\u7aef\u64cd\u4f5c\u3002<br>\u6ce8\u610f\uff1agcc \u5b58\u5728\u95ee\u9898\uff0c\u5373\u4f7f\u662f\u6700\u7b80\u5355\u7684hello world\u7a0b\u5e8f\uff0c\u7f16\u8bd1\u4e0d\u62a5\u9519\uff0c\u4e5f\u6ca1\u6709\u4efb\u4f55\u8f93\u51fa\u6587\u4ef6\u3002\u5f00\u59cb\u4ee5\u4e3a\u662f\u4ee3\u7801\u6587\u4ef6\u6216\u8005\u5b89\u88c5\u73af\u5883\u95ee\u9898\uff0cgithub\u4e0a\u6709\u591a\u4eba\u53cd\u6620\u76f8\u540c\u7684\u95ee\u9898\uff0c\u597d\u50cf\u81ea\u4ece\u7248\u672c14\u4e4b\u540e\u5c31\u5b58\u5728\u6b64\u95ee\u9898\uff0c\u73b0\u5728\u90fd\u523016\u4e86\uff0c\u95ee\u9898\u4f9d\u7136\u5b58\u5728\u3002<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code># gcc<br>pacman -S mingw-w64-x86_64-toolchain<br># clang<br>pacman -S mingw-w64-clang-x86_64-toolchain<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u5b89\u88c5python\u5305 MSYS2\u4e2d\u7684python\u548c\u7cfb\u7edf\u7684python\u5305\u4e0d\u540c\uff0c\u4e0d\u80fd\u6df7\u7528\uff0c\u5728MSYS2\u73af\u5883\u9700\u8981\u91cd\u65b0\u5b89\u88c5\u3002\u5e76\u4e14\u9700\u4f7f\u7528<code>pacman<\/code>\u5b89\u88c5\uff0c\u800c\u4e0d\u80fd\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5\u3002 \u4f8b\u5982\u5b89\u88c5\u00a0<code>setuptools<\/code><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>pacman -S mingw-w64-clang-x86_64-setuptools<br>pacman -Ss \u5173\u952e\u8bcd # \u641c\u7d22\u5305\u540d<br><\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u6253\u5305 \u7ecf\u9a8c\u8bc1\uff0c\u4f7f\u7528<code>clang<\/code>\u7f16\u8bd1\u6253\u5305\u53ef\u4ee5\u5728Windows\u4e0a\u8fd0\u884c\u3002\u6700\u5148\u4f7f\u7528\u7684<code>clang<\/code>\u7f16\u8bd1\u6253\u5305\uff0c\u6253\u5305\u540e\u63d0\u793a\u4e0d\u662f\u6709\u6548\u768432\u4f4d\u7a0b\u5e8f\uff0c\u6000\u7591\u662f<code>clang<\/code>\u7684\u95ee\u9898\uff0c\u540e\u6539\u4e3a<code>MSVC<\/code>\u7f16\u8bd1\u6253\u5305\uff0c\u8fd8\u662f\u63d0\u793a\u4e0d\u662f\u6709\u6548\u768432\u4f4d\u7a0b\u5e8f\u3002\u6700\u7ec8\u53d1\u73b0\u662f\u7531\u4e8e\u9759\u6001\u8d44\u6e90\u7684\u8def\u5f84\u5bfc\u81f4\u7684\u95ee\u9898\u3002\u89e3\u51b3\u8def\u5f84\u95ee\u9898\u540e\uff0c\u4e24\u79cd\u65b9\u5f0f\u6253\u5305\u5747\u53ef\u4ee5\u6b63\u5e38\u8fd0\u884c\uff0c\u4f7f\u7528<code>MSVC<\/code>\u7f16\u8bd1\u6253\u5305\u7684exe\u6587\u4ef6\u4f53\u79ef\u66f4\u5c0f\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 python \u4ee3\u7801\u7531\u4e8e\u662f\u811a\u672c\u8bed\u8a00\uff0c\u5982\u679c\u662f\u9700\u8981\u4fdd\u5bc6\u7684\u7b97\u6cd5\uff0c\u6700\u597d\u662f\u4f7f\u7528Cython\u5c06python\u4ee3\u7801\u7f16\u8bd1\u6210C &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/thereisno.top\/?p=2920\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cpython \u6253\u5305exe\u6587\u4ef6\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[325,322,326,323,324,321,9],"class_list":["post-2920","post","type-post","status-publish","format-standard","hentry","category-python","tag-clang","tag-cython","tag-gcc","tag-msvc","tag-msys2","tag-pyinstaller","tag-python"],"_links":{"self":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2920"}],"version-history":[{"count":1,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2920\/revisions"}],"predecessor-version":[{"id":2921,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2920\/revisions\/2921"}],"wp:attachment":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}