python 使用Pyinstaller打包成exe

windows+Pyinstaller py代码生成exe可执行文件

Pyinstaller

今天心血来潮,想试试py代码打包成exe执行,网上搜索了一大堆资料,选择了Pyinstaller来实操一把

安装

> pip install Pyinstaller
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple, http://pypi.douban.com/simple
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If th
is repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warnin
g and allow it anyway with '--trusted-host pypi.douban.com'.
Collecting Pyinstaller
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/3c/c9/c3f9bc64eb11eee6a824686deba6129884c8cbdf70e7506
61773b9865ee0/PyInstaller-3.6.tar.gz (3.5 MB)
     |████████████████████████████████| 3.5 MB 204 kB/s
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If th
is repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warnin
g and allow it anyway with '--trusted-host pypi.douban.com'.
Collecting altgraph
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ee/3d/bfca21174b162f6ce674953f1b7a640c1498357fa618477
6029557c25399/altgraph-0.17-py2.py3-none-any.whl (21 kB)
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If th
is repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warnin
g and allow it anyway with '--trusted-host pypi.douban.com'.
Collecting pywin32-ctypes>=0.2.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9e/4b/3ab2720f1fa4b4bc924ef1932b842edf10007e4547ea815
7b0b9fc78599a/pywin32_ctypes-0.2.0-py2.py3-none-any.whl (28 kB)
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If th
is repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warnin
g and allow it anyway with '--trusted-host pypi.douban.com'.
Collecting pefile>=2017.8.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/36/58/acf7f35859d541985f0a6ea3c34baaefbfaee23642cf11e
85fe36453ae77/pefile-2019.4.18.tar.gz (62 kB)
     |████████████████████████████████| 62 kB 128 kB/s
Requirement already satisfied: setuptools in d:\program\virtulenv\virenv\lib\site-packages (from Pyinstall
er) (46.1.3)
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If th
is repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warnin
g and allow it anyway with '--trusted-host pypi.douban.com'.
Collecting future
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/45/0b/38b06fd9b92dc2b68d58b75f900e97884c45bedd2ff8320
3d933cf5851c9/future-0.18.2.tar.gz (829 kB)
     |████████████████████████████████| 829 kB 181 kB/s
Building wheels for collected packages: Pyinstaller, pefile, future
  Building wheel for Pyinstaller (PEP 517) ... done
  Created wheel for Pyinstaller: filename=PyInstaller-3.6-py3-none-any.whl size=2926582 sha256=a569df08a0a7479
84001e7bfa24cf4f7968a8ffb894d915b158284237347b8ee
  Stored in directory: c:\users\xxxx\appdata\local\pip\cache\wheels\6d\cd\b7\5aa8c51f2f87e77b5a1b147a7538f76
96b4d86524a86570ef5
  Building wheel for pefile (setup.py) ... done
  Created wheel for pefile: filename=pefile-2019.4.18-py3-none-any.whl size=60827 sha256=7b96fd3760c19c2f2d2c6
af08f5524864a5e3b4cc2a936aba6eaab400850ef33
  Stored in directory: c:\users\xxx\appdata\local\pip\cache\wheels\35\b5\51\aa7b31a7c5e64c5800e9910fc4582cc
bb8cdc698434946f967
  Building wheel for future (setup.py) ... done
  Created wheel for future: filename=future-0.18.2-py3-none-any.whl size=491062 sha256=ad1f86605e9ad9ebd01aca4
8a2b23566134403a0dc96a1c095ba459832c8773c
  Stored in directory: c:\users\xxx\appdata\local\pip\cache\wheels\f3\f6\70\8b2bba44d8bf6ee5b7eac561416d9ef
5c28dd2320490fc0f1f
Successfully built Pyinstaller pefile future
Installing collected packages: altgraph, pywin32-ctypes, future, pefile, Pyinstaller
Successfully installed Pyinstaller-3.6 altgraph-0.17 future-0.18.2 pefile-2019.4.18 pywin32-ctypes-0.2.0

安装貌似很顺利,我们继续,看下Pyinstaller -h参数说明

> pyinstaller
usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME]
                   [--add-data <SRC;DEST or SRC:DEST>]
                   [--add-binary <SRC;DEST or SRC:DEST>] [-p DIR]
                   [--hidden-import MODULENAME]
                   [--additional-hooks-dir HOOKSPATH]
                   [--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES]
                   [--key KEY] [-d {all,imports,bootloader,noarchive}] [-s]
                   [--noupx] [--upx-exclude FILE] [-c] [-w]
                   [-i <FILE.ico or FILE.exe,ID or FILE.icns>]
                   [--version-file FILE] [-m <FILE or XML>] [-r RESOURCE]
                   [--uac-admin] [--uac-uiaccess] [--win-private-assemblies]
                   [--win-no-prefer-redirects]
                   [--osx-bundle-identifier BUNDLE_IDENTIFIER]
                   [--runtime-tmpdir PATH] [--bootloader-ignore-signals]
                   [--distpath DIR] [--workpath WORKPATH] [-y]
                   [--upx-dir UPX_DIR] [-a] [--clean] [--log-level LEVEL]
                   scriptname [scriptname ...]

结合网上资料,命令行生成exe,先来小试牛刀


>pyinstaller -F -w weibo_search.py
78 INFO: PyInstaller: 3.6
79 INFO: Python: 3.6.5
79 INFO: Platform: Windows-10-10.0.18362-SP0
80 INFO: wrote D:\Program\project\weibo_search.spec
85 INFO: UPX is not available.
88 INFO: Extending PYTHONPATH with paths
['D:\\Program\\project\',
 'D:\\Program\\project\']
89 INFO: checking Analysis
90 INFO: Building Analysis because Analysis-00.toc is non existent
90 INFO: Initializing module dependency graph...
93 INFO: Caching module graph hooks...
107 INFO: Analyzing base_library.zip ...
4144 INFO: Caching module dependency graph...
4260 INFO: running Analysis Analysis-00.toc
4270 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by d:\program\virtulenv\virenv\scripts\python.exe
4715 INFO: Analyzing D:\Program\project\weibo_search.py
5205 INFO: Processing pre-safe import module hook   urllib3.packages.six.moves
6676 INFO: Processing module hooks...
6677 INFO: Loading module hook "hook-encodings.py"...
6813 INFO: Loading module hook "hook-pydoc.py"...
6815 INFO: Loading module hook "hook-selenium.py"...
6829 INFO: Loading module hook "hook-xml.dom.domreg.py"...
6830 INFO: Loading module hook "hook-xml.py"...
6855 INFO: Looking for ctypes DLLs
6865 INFO: Analyzing run-time hooks ...
6877 INFO: Looking for dynamic libraries
7015 INFO: Looking for eggs
7015 INFO: Using Python library d:\program\virtulenv\virenv\scripts\python36.dll
7016 INFO: Found binding redirects:
[]
7022 INFO: Warnings written to D:\Program\project\\build\weibo_search\warn-weibo_
search.txt
7104 INFO: Graph cross-reference written to D:\Program\project\build\weibo_searc
h\xref-weibo_search.html
7139 INFO: checking PYZ
7140 INFO: Building PYZ because PYZ-00.toc is non existent
7141 INFO: Building PYZ (ZlibArchive) D:\Program\project\build\weibo_search\PYZ-
00.pyz
7909 INFO: Building PYZ (ZlibArchive) D:\Program\project\build\weibo_search\PYZ-
00.pyz completed successfully.
7928 INFO: checking PKG
**7929 INFO: Building PKG because PKG-00.toc is non existent**
7929 INFO: Building PKG (CArchive) PKG-00.pkg
9941 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
9945 INFO: Bootloader d:\program\virtulenv\virenv\lib\site-packages\PyInstaller\bootloader\Windows-32bit\r
unw.exe
9945 INFO: checking EXE
9946 INFO: Building EXE because EXE-00.toc is non existent
9946 INFO: Building EXE from EXE-00.toc
9946 INFO: Appending archive to EXE D:\Program\project\dist\weibo_search.exe
9954 INFO: Building EXE from EXE-00.toc completed successfully.

w ,制作窗口程序
F ,制作独立的可执行程序
发现了不少non existent,应该是缺少某个包

如果去掉w参数,再试试,果然成功

>pyinstaller -F  weibo_search.py
73 INFO: PyInstaller: 3.6
74 INFO: Python: 3.6.5
75 INFO: Platform: Windows-10-10.0.18362-SP0
76 INFO: wrote D:\Program\project\weibo_search.spec
80 INFO: UPX is not available.
82 INFO: Extending PYTHONPATH with paths
['D:\\Program\\project\',
 'D:\\Program\\project\']
84 INFO: checking Analysis
115 INFO: checking PYZ
137 INFO: checking PKG
143 INFO: Building because D:\Program\project\\build\weibo_search\weibo_search.ex
e.manifest changed
145 INFO: Building PKG (CArchive) PKG-00.pkg
1938 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
1943 INFO: Bootloader d:\program\virtulenv\virenv\lib\site-packages\PyInstaller\bootloader\Windows-32bit\r
un.exe
1944 INFO: checking EXE
1949 INFO: Building because console changed
1950 INFO: Building EXE from EXE-00.toc
1951 INFO: Appending archive to EXE D:\Program\project\\dist\weibo_search.exe
1962 INFO: Building EXE from EXE-00.toc completed successfully.

会在目录下生成dist文件夹,生成与脚本相同文件名的exe可执行文件,双击看看是什么效果
在这里插入图片描述
我们实现了输入关键字搜索,并拿到博主ID,及发布时间,转发评论点赞数
后续之作UI实现输入,查询并格式化输出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值