Python项目打包

最近写了一个python脚本,用来提取数据的

写好之后想着怎么打包成exe文件

现在把方法记录一下,同时也贴出来

 

1. 需要安装pyinstaller

下面有两个cmd,任用一条即可,第二条是从清华源下载的

D:\Program Files\Python_3_8_4>pip install pyinstaller
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)'))': /simple/pyinstaller/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)'))': /simple/pyinstaller/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)'))': /simple/pyinstaller/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)'))': /simple/pyinstaller/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)'))': /simple/pyinstaller/
Could not fetch URL https://pypi.org/simple/pyinstaller/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pyinstaller/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)'))) - skipping
ERROR: Could not find a version that satisfies the requirement pyinstaller (from versions: none)
ERROR: No matching distribution found for pyinstaller
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)'))) - skipping

D:\Program Files\Python_3_8_4>
D:\Program Files\Python_3_8_4>
D:\Program Files\Python_3_8_4>pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pyinstaller
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b6/27/a006fcadba0db30819c968eb8decb4937cda398ca7a44d8874172cdc228a/pyinstaller-4.3.tar.gz (3.7 MB)
     |████████████████████████████████| 3.7 MB 242 kB/s
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Collecting pyinstaller-hooks-contrib>=2020.6
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/27/c7/58a634d861e4744ac62dca4a4992ace8def8b05dab91e6b25e5043e79acf/pyinstaller_hooks_contrib-2021.1-py2.py3-none-any.whl (181 kB)
     |████████████████████████████████| 181 kB 6.8 MB/s
Collecting pefile>=2017.8.1; sys_platform == "win32"
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/36/58/acf7f35859d541985f0a6ea3c34baaefbfaee23642cf11e85fe36453ae77/pefile-2019.4.18.tar.gz (62 kB)
     |████████████████████████████████| 62 kB 4.5 MB/s
Collecting altgraph
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ee/3d/bfca21174b162f6ce674953f1b7a640c1498357fa6184776029557c25399/altgraph-0.17-py2.py3-none-any.whl (21 kB)
Requirement already satisfied: setuptools in d:\program files\python_3_8_4\lib\site-packages (from pyinstaller) (47.1.0)
Collecting pywin32-ctypes>=0.2.0; sys_platform == "win32"
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9e/4b/3ab2720f1fa4b4bc924ef1932b842edf10007e4547ea8157b0b9fc78599a/pywin32_ctypes-0.2.0-py2.py3-none-any.whl (28 kB)
Collecting future
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/45/0b/38b06fd9b92dc2b68d58b75f900e97884c45bedd2ff83203d933cf5851c9/future-0.18.2.tar.gz (829 kB)
     |████████████████████████████████| 829 kB 49 kB/s
Using legacy setup.py install for pefile, since package 'wheel' is not installed.
Using legacy setup.py install for future, since package 'wheel' is not installed.
Building wheels for collected packages: pyinstaller
  Building wheel for pyinstaller (PEP 517) ... done
  Created wheel for pyinstaller: filename=pyinstaller-4.3-py3-none-any.whl size=2457297 sha256=0a75c5bb9af26b4bc137911ebd319ade2bbc1c07de20a201a107e4d0b8257073
  Stored in directory: c:\users\mtk22164\appdata\local\pip\cache\wheels\15\04\59\4198de6f154312a2c984101362dedd9001a32c3095279b138b
Successfully built pyinstaller
Installing collected packages: pyinstaller-hooks-contrib, future, pefile, altgraph, pywin32-ctypes, pyinstaller
    Running setup.py install for future ... done
    Running setup.py install for pefile ... done
Successfully installed altgraph-0.17 future-0.18.2 pefile-2019.4.18 pyinstaller-4.3 pyinstaller-hooks-contrib-2021.1 pywin32-ctypes-0.2.0
WARNING: You are using pip version 20.1.1; however, version 21.1.1 is available.
You should consider upgrading via the 'd:\program files\python_3_8_4\python.exe -m pip install --upgrade pip' command.

 

2. 安装成功之后,用cmd进入到需要打包的py文件的目录,然后用pyinstaller -F .py文件名

如下

E:\MyProject\Pycharm\capther_data_2.0>pyinstaller -F read_keywords.py
482 INFO: PyInstaller: 4.3
482 INFO: Python: 3.8.4
483 INFO: Platform: Windows-10-10.0.17763-SP0
484 INFO: wrote E:\MyProject\Pycharm\capther_data_2.0\read_keywords.spec
486 INFO: UPX is not available.
489 INFO: Extending PYTHONPATH with paths
['E:\\MyProject\\Pycharm\\capther_data_2.0',
 'E:\\MyProject\\Pycharm\\capther_data_2.0']
498 INFO: checking Analysis
498 INFO: Building Analysis because Analysis-00.toc is non existent
499 INFO: Initializing module dependency graph...
501 INFO: Caching module graph hooks...
514 WARNING: Several hooks defined for module 'win32ctypes.core'. Please take care they do not conflict.
519 INFO: Analyzing base_library.zip ...
3950 INFO: Processing pre-find module path hook distutils from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-d
istutils.py'.
3952 INFO: distutils: retargeting to non-venv dir 'd:\\program files\\python_3_8_4\\lib'
7616 INFO: Caching module dependency graph...
7875 INFO: running Analysis Analysis-00.toc
7905 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by d:\program files\python_3_8_4\python.exe
8744 INFO: Analyzing E:\MyProject\Pycharm\capther_data_2.0\read_keywords.py
9867 INFO: Processing pre-find module path hook site from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-site.p
y'.
9868 INFO: site: retargeting to fake-dir 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\fake-modules'
10982 INFO: Processing pre-safe import module hook setuptools.extern.six.moves from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks\\pre_saf
e_import_module\\hook-setuptools.extern.six.moves.py'.
16684 INFO: Processing module hooks...
16684 INFO: Loading module hook 'hook-openpyxl.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
16853 INFO: Loading module hook 'hook-win32ctypes.core.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...

17030 INFO: Loading module hook 'hook-difflib.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
17033 INFO: Loading module hook 'hook-distutils.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
17034 INFO: Loading module hook 'hook-distutils.util.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
17037 INFO: Loading module hook 'hook-encodings.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
17144 INFO: Loading module hook 'hook-heapq.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
17147 INFO: Loading module hook 'hook-lib2to3.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
17415 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
17418 INFO: Loading module hook 'hook-numpy.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
17479 INFO: Import to be excluded not found: 'f2py'
17501 INFO: Loading module hook 'hook-numpy._pytesttester.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
17505 INFO: Loading module hook 'hook-pickle.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
17509 INFO: Loading module hook 'hook-pkg_resources.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
17914 INFO: Processing pre-safe import module hook win32com from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\pre_safe_im
port_module\\hook-win32com.py'.
18237 WARNING: Hidden import "pkg_resources.markers" not found!
18243 INFO: Loading module hook 'hook-setuptools.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
19053 INFO: Loading module hook 'hook-sysconfig.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
19054 INFO: Loading module hook 'hook-xml.dom.domreg.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
19055 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
19056 INFO: Loading module hook 'hook-xml.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
19056 INFO: Loading module hook 'hook-_tkinter.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
19516 INFO: checking Tree
19516 INFO: Building Tree because Tree-00.toc is non existent
19516 INFO: Building Tree Tree-00.toc
19725 INFO: checking Tree
19725 INFO: Building Tree because Tree-01.toc is non existent
19725 INFO: Building Tree Tree-01.toc
19846 INFO: checking Tree
19846 INFO: Building Tree because Tree-02.toc is non existent
19846 INFO: Building Tree Tree-02.toc
19868 INFO: Loading module hook 'hook-setuptools.msvc.py' from 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks'...
19901 INFO: Looking for ctypes DLLs
19979 INFO: Analyzing run-time hooks ...
19984 INFO: Including run-time hook 'd:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
19997 INFO: Looking for dynamic libraries
20788 INFO: Looking for eggs
20788 INFO: Using Python library d:\program files\python_3_8_4\python38.dll
20789 INFO: Found binding redirects:
[]
20796 INFO: Warnings written to E:\MyProject\Pycharm\capther_data_2.0\build\read_keywords\warn-read_keywords.txt
20905 INFO: Graph cross-reference written to E:\MyProject\Pycharm\capther_data_2.0\build\read_keywords\xref-read_keywords.html
20936 INFO: checking PYZ
20937 INFO: Building PYZ because PYZ-00.toc is non existent
20937 INFO: Building PYZ (ZlibArchive) E:\MyProject\Pycharm\capther_data_2.0\build\read_keywords\PYZ-00.pyz
21942 INFO: Building PYZ (ZlibArchive) E:\MyProject\Pycharm\capther_data_2.0\build\read_keywords\PYZ-00.pyz completed successfully.
21981 INFO: checking PKG
21982 INFO: Building PKG because PKG-00.toc is non existent
21982 INFO: Building PKG (CArchive) PKG-00.pkg
30626 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
30630 INFO: Bootloader d:\program files\python_3_8_4\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
30631 INFO: checking EXE
30631 INFO: Building EXE because EXE-00.toc is non existent
30631 INFO: Building EXE from EXE-00.toc
30633 INFO: Copying icons from ['d:\\program files\\python_3_8_4\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
30640 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
30640 INFO: Writing RT_ICON 1 resource with 3752 bytes
30641 INFO: Writing RT_ICON 2 resource with 2216 bytes
30641 INFO: Writing RT_ICON 3 resource with 1384 bytes
30641 INFO: Writing RT_ICON 4 resource with 37019 bytes
30641 INFO: Writing RT_ICON 5 resource with 9640 bytes
30642 INFO: Writing RT_ICON 6 resource with 4264 bytes
30642 INFO: Writing RT_ICON 7 resource with 1128 bytes
30646 INFO: Updating manifest in E:\MyProject\Pycharm\capther_data_2.0\build\read_keywords\run.exe.di_jauk7
30648 INFO: Updating resource type 24 name 1 language 0
30651 INFO: Appending archive to EXE E:\MyProject\Pycharm\capther_data_2.0\dist\read_keywords.exe
38075 INFO: Building EXE from EXE-00.toc completed successfully.

 

3. 这里即生成了.exe文件,文件放在同.py文件同级的目录下,会生成一个新文件夹 \dist,里面有.exe.文件

把这个.exe发布即可,如果程序还依赖其他文件,则把其他文件一并发布

 

这里是只针对一个.py文件的打包方法,如果有多个.py文件,则是其他方法

打包多个.py文件的方式,后面再更新

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值