{"id":531,"date":"2020-11-02T03:20:43","date_gmt":"2020-11-02T03:20:43","guid":{"rendered":"https:\/\/blog.kaispace.cn\/?p=531"},"modified":"2020-11-02T03:20:43","modified_gmt":"2020-11-02T03:20:43","slug":"python%e5%8f%af%e6%89%a7%e8%a1%8c%e6%89%93%e5%8c%85-pyinstaller","status":"publish","type":"post","link":"https:\/\/blog.kaispace.cn\/?p=531","title":{"rendered":"Python\u53ef\u6267\u884c\u6253\u5305-Pyinstaller"},"content":{"rendered":"<h1>Python\u53ef\u6267\u884c\u6253\u5305<\/h1>\n<p>\u7531\u4e8e\u4e0d\u540c\u9879\u76ee\u4f7f\u7528\u5230\u4e0d\u540c\u7248\u672c\u7684Python, \u5230\u65b0\u670d\u52a1\u5668\u90e8\u7f72\u7684\u65f6\u5019\u8fd8\u9700\u8981\u5b89\u88c5\u73af\u5883\uff0c\u5728\u6ca1\u6709\u7528\u5bb9\u5668\u524d\uff0c\u90fd\u663e\u5f97\u683c\u5916\u75db\u82e6\u3002<br \/>\n\u56e0\u6b64\u5c31\u9700\u8981\u6253\u5305Python\u9879\u76ee\u5b9e\u73b0\u4efb\u610f\u73af\u5883\u65e0\u4f9d\u8d56\u90e8\u7f72\u3002<\/p>\n<p>\u8fd9\u91cc\u4f7f\u7528\u5230\u7684\u5de5\u5177\u53eb<font color='red'>Pyinstaller<\/font>,\u8fd9\u91cc\u662f\u57fa\u4e8ePython3.8\u6d4b\u8bd5\uff0c\u7406\u8bba\u4e0aPython2\u4e5f\u53ef\u4ee5\u5b9e\u73b0<\/p>\n<h2>1. \u524d\u7f6e\u6761\u4ef6<\/h2>\n<h3>1.1 \u5df2\u5b89\u88c5Python\u4e14\u7f16\u8bd1\u65f6\u6709\u8bbe\u7f6e&#8211;enable-shared<\/h3>\n<p>\u5982\u679c\u6ca1\u6709&#8211;enable-shared\uff0c\u5728\u6253\u5305\u65f6\u5c31\u4f1a\u62a5\u9519\uff0c\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u7f16\u8bd1\u65f6\u52a0\u4e0a<\/p>\n<pre><code class=\"language-shell line-numbers\">.\/configure --with-openssl=\/usr --enable-shared --prefix=\/usr &amp;&amp; make &amp;&amp; make install\n<\/code><\/pre>\n<p>\u5f53\u7136\u5982\u679c\u662f\u5728\u6784\u5efa\u673a\u5668\uff0c\u5efa\u8bae\u4f7f\u7528<font color='red'>Pyenv<\/font>\uff0c\u7531\u4e8e\u5185\u7f51git\u4e0d\u540c\uff0c\u6240\u4ee5\u9700\u8981\u81ea\u884c\u4e0b\u8f7d<\/p>\n<pre><code class=\"language-shell line-numbers\"># git clone https:\/\/github.com\/pyenv\/pyenv.git ~\/.pyenv\nunzip pyenv.zip\necho \"export PYTHON_CONFIGURE_OPTS=--enable-shared\" &gt;&gt; \/etc\/profile\nsource \/etc\/profile\nmv pyenv-master .pyenv\necho 'export PATH=~\/.pyenv\/bin:<span class=\"katex math inline\">PATH' &gt;&gt; ~\/.bashrc\necho 'export PYENV_ROOT=~\/.pyenv' &gt;&gt; ~\/.bashrc\necho 'eval \"<\/span>(pyenv init -)\"' &gt;&gt; ~\/.bashrc\nsource ~\/.bashrc\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u597d\u50cfsource\u5e76\u4e0d\u751f\u6548\uff0c\u5982\u679c\u6267\u884cpyenv\u663e\u793a\u4e0d\u5b58\u5728\uff0c\u5219\u91cd\u65b0<br \/>\n&#8220;`source ~\/.bashrc&#8220;`, \u8fd8\u6709&#8220;`env | grep PYTHON_CONFIGURE_OPTS&#8220;`\u4e00\u4e0b\u770b\u770b\u73af\u5883\u662f\u5426\u6210\u529f\u5931\u6548<br \/>\n\u7136\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528<\/p>\n<pre><code class=\"language-shell line-numbers\">pyenv install 3.8.5\npyenv global 3.8.5\n<\/code><\/pre>\n<p>\u4efb\u610f\u7684\u5207\u6362\u4f7f\u7528python\u4e86\uff0c<font color='red'>\u6ce8\u610f\u4e00\u5b9a\u8981\u5207\u6362<\/font><\/p>\n<h3>1.2 \u5b89\u88c5\u9879\u76ee\u9700\u8981\u7684\u5305<\/h3>\n<pre><code class=\"language-shell line-numbers\"># \u5230\u81ea\u5df1\u9879\u76ee\u8fdb\u884cpython -m freeze &gt; requirement.txt dump\u4e00\u4e0b\u4f9d\u8d56\u5305\n# \u5b89\u88c5\u9879\u76ee\u7684\u5305\npip3 install -r requirement.txt --index-url https:\/\/mirrors.cloud.tencent.com\/pypi\/simple\/  --extra-index-url https:\/\/mirrors.tencent.com\/repository\/pypi\/tencent_pypi\/simple\/\npip3 install pyinstaller --index-url https:\/\/mirrors.cloud.tencent.com\/pypi\/simple\/\n<\/code><\/pre>\n<h2>2. \u4f7f\u7528<\/h2>\n<pre><code class=\"language-shell line-numbers\">pyinstaller --onefile --clean YOUR_PYTHON_FILE.py   --- \u6253\u5305\u4e3a\u4e00\u6574\u4e2a\u6587\u4ef6\n\u6216\u8005\npyinstaller --clean YOUR_PYTHON_FILE.py             --- \u6253\u5305\u4e3a\u6587\u4ef6\u5939\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u8fd9\u91cc\u6253\u5305\u6210\u4e00\u6574\u4e2a\u6587\u4ef6\u65f6\uff0c\u6267\u884c\u4f1a\u51fa\u73b02\u4e2a\u8fdb\u7a0b\uff0c\u8fd9\u662f\u56e0\u4e3a\u9700\u8981\u7236\u8fdb\u7a0b\u53bb\u6784\u5efa\u4e00\u4e2a\u865a\u62df\u7684Python\u73af\u5883\uff0c\u7136\u540e\u5728\u73af\u5883\u4e2d\u6267\u884c\u771f\u6b63\u7684\u7a0b\u5e8f<\/p>\n<p>\u6253\u5305\u540e\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5728dist\u6587\u4ef6\u5939\u4e2d\uff0c\u53ea\u8981\u6743\u9650\u6709x\uff0c\u5219\u53ef\u4ee5\u76f4\u63a5<br \/>\n&#8220;`.\/EXEC_FILE&#8220;`\u6267\u884c\u6587\u4ef6<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u53ef\u6267\u884c\u6253\u5305 \u7531\u4e8e\u4e0d\u540c\u9879\u76ee\u4f7f\u7528\u5230\u4e0d\u540c\u7248\u672c\u7684Python, \u5230\u65b0\u670d\u52a1\u5668\u90e8\u7f72\u7684\u65f6\u5019\u8fd8\u9700\u8981\u5b89\u88c5\u73af\u5883\uff0c\u5728\u6ca1\u6709&#8230;<\/p>\n","protected":false},"author":1,"featured_media":486,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,15],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts\/531"}],"collection":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=531"}],"version-history":[{"count":1,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts\/531\/revisions"}],"predecessor-version":[{"id":532,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts\/531\/revisions\/532"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/media\/486"}],"wp:attachment":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}