python篇---在windows下利用pyinstaller打包成exe

安装pyinstaller

pip install pyinstaller

pip install https://github.com/pyinstaller/pyinstaller/archive/develop.tar.gz

项目打包

pyinstaller -F app_test_time.py
(pytorch_gpu) D:\Project\object-detections>pyinstaller -F app_test_time.py
350 INFO: PyInstaller: 5.0.dev0
350 INFO: Python: 3.7.10 (conda)
351 INFO: Platform: Windows-10-10.0.17763-SP0
352 INFO: wrote D:\Project\object-detections\app_test_time.spec
354 INFO: UPX is not available.
355 INFO: Extending PYTHONPATH with paths
['D:\\Project\\object-detections']
608 INFO: checking Analysis
608 INFO: Building Analysis because Analysis-00.toc is non existent
609 INFO: Initializing module dependency graph...
610 INFO: Caching module graph hooks...
621 INFO: Analyzing base_library.zip ...
2008 INFO: Processing pre-find module path hook distutils from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
2008 INFO: distutils: retargeting to non-venv dir 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib'
2967 INFO: Caching module dependency graph...
3103 INFO: running Analysis Analysis-00.toc
3105 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by d:\programs_con\conda3\envs\pytorch_gpu\python.exe
3304 INFO: Analyzing D:\Project\object-detections\app_test_time.py
5540 INFO: Processing pre-find module path hook site from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-site.py'.
5541 INFO: site: retargeting to fake-dir 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\fake-modules'
11201 INFO: Processing pre-safe import module hook urllib3.packages.six.moves from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-urllib3.packages.six.moves.py'.
14116 INFO: Processing pre-safe import module hook six.moves from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-six.moves.py'.
16986 INFO: Processing module hooks...
16986 INFO: Loading module hook 'hook-apscheduler.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
17328 INFO: Loading module hook 'hook-certifi.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
17333 INFO: Loading module hook 'hook-cv2.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
17336 INFO: Loading module hook 'hook-torch.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
17539 INFO: Loading module hook 'hook-difflib.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
17543 INFO: Loading module hook 'hook-distutils.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
17543 INFO: Loading module hook 'hook-distutils.util.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
17547 INFO: Loading module hook 'hook-encodings.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
17613 INFO: Loading module hook 'hook-heapq.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
17616 INFO: Loading module hook 'hook-importlib_metadata.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
17618 INFO: Loading module hook 'hook-lib2to3.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
17662 INFO: Loading module hook 'hook-matplotlib.backends.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
17663 INFO: Matplotlib backend selection method: automatic discovery of used backends
18142 INFO: Trying determine the default backend as first importable candidate from the list: ['Qt5Agg', 'Gtk3Agg', 'TkAgg', 'WxAgg']
19123 INFO: Selected matplotlib backends: ['TkAgg']
19204 INFO: Loading module hook 'hook-matplotlib.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
19513 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
19517 INFO: Loading module hook 'hook-numpy.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
19564 WARNING: Conda distribution 'setuptools', dependency of 'numpy', was not found. If you installed this distribution with pip then you may ignore this warning.
19565 WARNING: Conda distribution 'numpy-base', dependency of 'numpy', was not found. If you installed this distribution with pip then you may ignore this warning.
19585 INFO: Import to be excluded not found: 'f2py'
19603 INFO: Loading module hook 'hook-numpy._pytesttester.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
19606 INFO: Loading module hook 'hook-pickle.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
19608 INFO: Loading module hook 'hook-PIL.Image.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
20032 INFO: Loading module hook 'hook-PIL.ImageFilter.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
20034 INFO: Loading module hook 'hook-PIL.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
20044 INFO: Loading module hook 'hook-PIL.SpiderImagePlugin.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
20046 INFO: Loading module hook 'hook-pkg_resources.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
20509 INFO: Processing pre-safe import module hook win32com from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\pre_safe_import_module\\hook-win32com.py'.
20763 WARNING: Hidden import "pkg_resources.py2_warn" not found!
20763 WARNING: Hidden import "pkg_resources.markers" not found!
20766 INFO: Loading module hook 'hook-pytz.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
20947 INFO: Loading module hook 'hook-setuptools.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
21728 INFO: Loading module hook 'hook-sqlite3.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
21792 INFO: Loading module hook 'hook-sysconfig.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
21792 INFO: Loading module hook 'hook-win32ctypes.core.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
22023 INFO: Loading module hook 'hook-xml.dom.domreg.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
22024 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
22024 INFO: Loading module hook 'hook-xml.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
22025 INFO: Loading module hook 'hook-_tkinter.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
22133 INFO: checking Tree
22133 INFO: Building Tree because Tree-00.toc is non existent
22133 INFO: Building Tree Tree-00.toc
22210 INFO: checking Tree
22210 INFO: Building Tree because Tree-01.toc is non existent
22210 INFO: Building Tree Tree-01.toc
22274 INFO: checking Tree
22275 INFO: Building Tree because Tree-02.toc is non existent
22275 INFO: Building Tree Tree-02.toc
22280 INFO: Loading module hook 'hook-pythoncom.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
22458 INFO: Loading module hook 'hook-pywintypes.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
22628 INFO: Loading module hook 'hook-win32com.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
22975 INFO: Loading module hook 'hook-setuptools.msvc.py' from 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks'...
23025 INFO: Looking for ctypes DLLs
23069 INFO: Analyzing run-time hooks ...
23075 INFO: Including run-time hook 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgutil.py'
23077 INFO: Including run-time hook 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
23077 INFO: Including run-time hook 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_win32api.py'
23078 INFO: Including run-time hook 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth__tkinter.py'
23078 INFO: Including run-time hook 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_mplconfig.py'
23079 INFO: Including run-time hook 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\rthooks\\pyi_rth_certifi.py'
23080 INFO: Including run-time hook 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
23081 INFO: Including run-time hook 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgres.py'
23084 INFO: Including run-time hook 'd:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_win32comgenpy.py'
23097 INFO: Looking for dynamic libraries
5171 WARNING: lib not found: torch_python.dll dependency of d:\programs_con\conda3\envs\pytorch_gpu\lib\site-packages\torch\_C.cp37-win_amd64.pyd
28944 INFO: Looking for eggs
28944 INFO: Using Python library d:\programs_con\conda3\envs\pytorch_gpu\python37.dll
28944 INFO: Found binding redirects:
[]
28973 INFO: Warnings written to D:\Project\object-detections\build\app_test_time\warn-app_test_time.txt
29103 INFO: Graph cross-reference written to D:\Project\object-detections\build\app_test_time\xref-app_test_time.html
29250 INFO: checking PYZ
29250 INFO: Building PYZ because PYZ-00.toc is non existent
29250 INFO: Building PYZ (ZlibArchive) D:\Project\object-detections\build\app_test_time\PYZ-00.pyz
30716 INFO: Building PYZ (ZlibArchive) D:\Project\object-detections\build\app_test_time\PYZ-00.pyz completed successfully.
30751 INFO: checking PKG
30752 INFO: Building PKG because PKG-00.toc is non existent
30752 INFO: Building PKG (CArchive) app_test_time.pkg
362165 INFO: Building PKG (CArchive) app_test_time.pkg completed successfully.
362265 INFO: Bootloader d:\programs_con\conda3\envs\pytorch_gpu\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
362265 INFO: checking EXE
362266 INFO: Building EXE because EXE-00.toc is non existent
362266 INFO: Building EXE from EXE-00.toc
362267 INFO: Copying bootloader EXE to D:\Project\object-detections\dist\app_test_time.exe
362324 INFO: Copying icon to EXE
362325 INFO: Copying icons from ['d:\\programs_con\\conda3\\envs\\pytorch_gpu\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
362329 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
362329 INFO: Writing RT_ICON 1 resource with 3752 bytes
362329 INFO: Writing RT_ICON 2 resource with 2216 bytes
362329 INFO: Writing RT_ICON 3 resource with 1384 bytes
362329 INFO: Writing RT_ICON 4 resource with 37019 bytes
362330 INFO: Writing RT_ICON 5 resource with 9640 bytes
362330 INFO: Writing RT_ICON 6 resource with 4264 bytes
362330 INFO: Writing RT_ICON 7 resource with 1128 bytes
362373 INFO: Copying 0 resources to EXE
362373 INFO: Emedding manifest in EXE
362375 INFO: Updating manifest in D:\Project\object-detections\dist\app_test_time.exe
362375 INFO: Updating resource type 24 name 1 language 0
362416 INFO: Appending PKG archive to EXE
614148 INFO: Building EXE from EXE-00.toc completed successfully.
-h,–help查看该模块的帮助信息
-F,-onefile产生单个的可执行文件
-D,–onedir产生一个目录(包含多个文件)作为可执行程序
-a,–ascii不包含 Unicode 字符集支持
-d,–debug产生 debug 版本的可执行文件
-w,–windowed,–noconsolc指定程序运行时不显示命令行窗口(仅对 Windows 有效)
-c,–nowindowed,–console指定使用命令行窗口运行程序(仅对 Windows 有效)
-o DIR,–out=DIR指定 spec 文件的生成目录。如果没有指定,则默认使用当前目录来生成 spec 文件
-p DIR,–path=DIR设置 Python 导入模块的路径(和设置 PYTHONPATH 环境变量的作用相似)。也可使用路径分隔符(Windows 使用分号,Linux 使用冒号)来分隔多个路径
-n NAME,–name=NAME指定项目(产生的 spec)名字。如果省略该选项,那么第一个脚本的主文件名将作为 spec 的名字

问题汇总

1. 打开成功后的exe打开闪退

  • 进入dist文件夹
  • 打开cmd
  • 输入
  1. 第一种方式
.\app_test_time.py
  1. 第二种方式
    在代码末尾加上这句话 os.system("pause")
os.system("pause")

即可解决

2. pyinstaller 缺少"C:\Users\admin\AppData\Local\Temp"

https://gist.github.com/GaryLee/d191f239acf2f47bec93在这里插入图片描述
命名一个hook-torch.py的脚本
输入以下的代码

from PyInstaller.utils.hooks import collect_data_files

datas = collect_data_files('torch')

如图所示:
在这里插入图片描述
保存将此py文件放入Pyinstaller下的hook的文件夹中
在这里插入图片描述
[参考]
https://www.jianshu.com/p/fe9ee823501c

打包exe的代码

pyinstaller.exe --onefile --additional-hooks-dir=. --hidden-import=queue -w -F app_test_time.py

3. WARNING: file already exists but should not: C:\Users\workAI\AppData\Local\Temp_MEI132522\torch_C

此bug是使用pyinstaller编译文件为exe文件中产生的,具体如下:

WARNING: file already exists but should not: C:\Users\workAI\AppData\Local\Temp\_MEI132522\torch\_C

上述bug在编译和运行过程中没有产生影响,
具体操作如下:首先找到.spec配置文件修改配置文件,在配置文件中加入以下代码:

for d in a.datas:
	if '_C.cp37-win_amd64.pyd' in d[0]:
		a.datas.remove(d)
		break

详细的.spec配置文件的内容如下:

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(
    ['app_test_time.py'],
    pathex=[],
    binaries=[],
    datas=[],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)

for d in a.datas:
	if '_C.cp37-win_amd64.pyd' in d[0]:
		a.datas.remove(d)
		break
		
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='app_test_time',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

若pycharm的终端出问题,修改过程

将File -> Settings -> Tools -> Terminal中的值替换为Shell path
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心惠天意

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值