Windows环境使用 PyInstaller 打包python .py文件 安装和使用教程(保姆级别-小白版)

看了大多数文章,对于这个问题的描述 都是介绍不完整,没有展示结果。

一下记录我操作的完整过程供参考:

1,CMD安装环境:

pip install pyinstaller

执行过程:

C:\WINDOWS\system32>pip install pyinstaller
Collecting pyinstaller
  Downloading pyinstaller-4.0.tar.gz (3.5 MB)
     |████████████████████████████████| 3.5 MB 915 kB/s
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Collecting altgraph
  Downloading altgraph-0.17-py2.py3-none-any.whl (21 kB)
Collecting pyinstaller-hooks-contrib>=2020.6
  Downloading pyinstaller_hooks_contrib-2020.9-py2.py3-none-any.whl (163 kB)
     |████████████████████████████████| 163 kB 328 kB/s
Requirement already satisfied: pywin32-ctypes>=0.2.0; sys_platform == "win32" in d:\users\tianya\anaconda3\lib\site-packages (from pyinstaller) (0.2.0)
Requirement already satisfied: setuptools in d:\users\tianya\anaconda3\lib\site-packages (from pyinstaller) (49.2.0.post20200714)
Collecting pefile>=2017.8.1; sys_platform == "win32"
  Downloading pefile-2019.4.18.tar.gz (62 kB)
     |████████████████████████████████| 62 kB 103 kB/s
Requirement already satisfied: future in d:\users\tianya\anaconda3\lib\site-packages (from pefile>=2017.8.1; sys_platform == "win32"->pyinstaller) (0.18.2)
Building wheels for collected packages: pyinstaller, pefile
  Building wheel for pyinstaller (PEP 517) ... done
  Created wheel for pyinstaller: filename=pyinstaller-4.0-py3-none-any.whl size=2789243 sha256=1cf5c252709de68ffd58576012ffca2943196a6880bccdbb500e8733141954bf
  Stored in directory: c:\users\tianya\appdata\local\pip\cache\wheels\9b\28\8f\f51ae22c4391bbeb36c8fdc68203b0c094445b4db824a6ab7d
  Building wheel for pefile (setup.py) ... done
  Created wheel for pefile: filename=pefile-2019.4.18-py3-none-any.whl size=60827 sha256=6c7d450e986f951c3740525c39fb845d8adcf78c619c12d09d65aea54c10d0d8
  Stored in directory: c:\users\tianya\appdata\local\pip\cache\wheels\42\52\d5\9550bbfb9eeceaf0f19db1cf651cc8ba41d3bcf8b4d20e4279
Successfully built pyinstaller pefile
Installing collected packages: altgraph, pyinstaller-hooks-contrib, pefile, pyinstaller
Successfully installed altgraph-0.17 pefile-2019.4.18 pyinstaller-4.0 pyinstaller-hooks-contrib-2020.9

安装成功,接下来进行下一步。

2.打包文件:

我的测试程序是简单的opecv人脸识别测试文件,使用命令执行打包为.EXE 

命令为:pyinstaller -F  OpenCV-Python-faces.py 

以下是执行过程:

E:\fish AI\py--exe-测试>pyinstaller -F  OpenCV-Python-faces.py
151 INFO: PyInstaller: 4.0
151 INFO: Python: 3.8.3 (conda)
153 INFO: Platform: Windows-10-10.0.18362-SP0
181 INFO: wrote E:\fish AI\py--exe-测试\OpenCV-Python-faces.spec
190 INFO: UPX is not available.
205 INFO: Extending PYTHONPATH with paths
['E:\\fish AI\\py--exe-测试', 'E:\\fish AI\\py--exe-测试']
301 INFO: checking Analysis
302 INFO: Building Analysis because Analysis-00.toc is non existent
303 INFO: Initializing module dependency graph...
320 INFO: Caching module graph hooks...
342 INFO: Analyzing base_library.zip ...
10645 INFO: Processing pre-find module path hook distutils from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
10647 INFO: distutils: retargeting to non-venv dir 'd:\\users\\tianya\\anaconda3\\lib'
20433 INFO: Caching module dependency graph...
20797 INFO: running Analysis Analysis-00.toc
20915 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by d:\users\tianya\anaconda3\python.exe
22647 INFO: Analyzing E:\fish AI\py--exe-测试\OpenCV-Python-faces.py
27254 INFO: Processing pre-find module path hook site from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-site.py'.
27255 INFO: site: retargeting to fake-dir 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\fake-modules'
30679 INFO: Processing pre-safe import module hook setuptools.extern.six.moves from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-setuptools.extern.six.moves.py'.
55850 INFO: Processing module hooks...
55850 INFO: Loading module hook 'hook-cv2.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
55874 INFO: Loading module hook 'hook-pycparser.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
55876 INFO: Loading module hook 'hook-pytest.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
61122 INFO: Loading module hook 'hook-distutils.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
61125 INFO: Loading module hook 'hook-encodings.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
61300 INFO: Loading module hook 'hook-importlib_metadata.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
61337 INFO: Loading module hook 'hook-lib2to3.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
61564 INFO: Loading module hook 'hook-numpy.core.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
61803 INFO: MKL libraries found when importing numpy. Adding MKL to binaries
61833 INFO: Loading module hook 'hook-numpy.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
61835 INFO: Loading module hook 'hook-packaging.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
61838 INFO: Loading module hook 'hook-pkg_resources.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
63761 INFO: Processing pre-safe import module hook win32com from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\pre_safe_import_module\\hook-win32com.py'.
64718 WARNING: Hidden import "pkg_resources.py2_warn" not found!
65085 INFO: Processing pre-safe import module hook six.moves from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-six.moves.py'.
65210 WARNING: Hidden import "pkg_resources.markers" not found!
65213 INFO: Excluding import '__main__'
65216 INFO:   Removing import of __main__ from module pkg_resources
65217 INFO: Loading module hook 'hook-pygments.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
70843 INFO: Loading module hook 'hook-PyQt5.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
152862 WARNING: Hidden import "PyQt5.sip" not found!
152862 INFO: Loading module hook 'hook-PyQt5.QtCore.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
153315 INFO: Loading module hook 'hook-PyQt5.QtGui.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
153737 INFO: Loading module hook 'hook-scipy.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
153750 INFO: Loading module hook 'hook-setuptools.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
d:\users\tianya\anaconda3\lib\site-packages\setuptools\distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.
  warnings.warn(
155756 INFO: Loading module hook 'hook-sysconfig.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
155759 INFO: Loading module hook 'hook-wcwidth.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
155770 INFO: Loading module hook 'hook-xml.dom.domreg.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
155772 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
155773 INFO: Loading module hook 'hook-xml.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
155775 INFO: Loading module hook 'hook-_tkinter.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
156283 INFO: checking Tree
156283 INFO: Building Tree because Tree-00.toc is non existent
156284 INFO: Building Tree Tree-00.toc
156582 INFO: checking Tree
156582 INFO: Building Tree because Tree-01.toc is non existent
156583 INFO: Building Tree Tree-01.toc
156622 INFO: Loading module hook 'hook-pythoncom.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
157882 INFO: Loading module hook 'hook-pywintypes.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
159128 INFO: Loading module hook 'hook-win32com.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
d:\users\tianya\anaconda3\lib\site-packages\win32com\client\makepy.py:369: SyntaxWarning: "is not" with a literal. Did you mean "!="?
  if path is not '' and not os.path.exists(path):
160981 INFO: Loading module hook 'hook-PIL.Image.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
162151 INFO: Loading module hook 'hook-PIL.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
162155 INFO: Excluding import 'PyQt5'
162159 INFO:   Removing import of PyQt5.QtCore from module PIL.ImageQt
162160 INFO:   Removing import of PyQt5.QtGui from module PIL.ImageQt
162162 INFO: Import to be excluded not found: 'FixTk'
162163 INFO: Excluding import 'tkinter'
162168 INFO:   Removing import of tkinter from module PIL.ImageTk
162169 INFO: Import to be excluded not found: 'PySide'
162169 INFO: Import to be excluded not found: 'PyQt4'
162170 INFO: Loading module hook 'hook-PIL.SpiderImagePlugin.py' from 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks'...
162176 INFO: Import to be excluded not found: 'FixTk'
162177 INFO: Excluding import 'tkinter'
162276 INFO: Looking for ctypes DLLs
162502 INFO: Analyzing run-time hooks ...
162517 INFO: Including run-time hook 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth__tkinter.py'
162520 INFO: Including run-time hook 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
162527 INFO: Including run-time hook 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgres.py'
162531 INFO: Including run-time hook 'd:\\users\\tianya\\anaconda3\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_win32comgenpy.py'
162566 INFO: Looking for dynamic libraries
163227 WARNING: lib not found: msmpi.dll dependency of d:\users\tianya\anaconda3\Library\bin\mkl_blacs_msmpi_lp64.dll
163328 WARNING: lib not found: pgf90rtl.dll dependency of d:\users\tianya\anaconda3\Library\bin\mkl_pgi_thread.dll
163334 WARNING: lib not found: pgf90.dll dependency of d:\users\tianya\anaconda3\Library\bin\mkl_pgi_thread.dll
163340 WARNING: lib not found: pgc14.dll dependency of d:\users\tianya\anaconda3\Library\bin\mkl_pgi_thread.dll
163449 WARNING: lib not found: impi.dll dependency of d:\users\tianya\anaconda3\Library\bin\mkl_blacs_intelmpi_ilp64.dll
163681 WARNING: lib not found: mpich2mpi.dll dependency of d:\users\tianya\anaconda3\Library\bin\mkl_blacs_mpich2_lp64.dll
163706 WARNING: lib not found: msmpi.dll dependency of d:\users\tianya\anaconda3\Library\bin\mkl_blacs_msmpi_ilp64.dll
164248 WARNING: lib not found: mpich2mpi.dll dependency of d:\users\tianya\anaconda3\Library\bin\mkl_blacs_mpich2_ilp64.dll
164307 WARNING: lib not found: impi.dll dependency of d:\users\tianya\anaconda3\Library\bin\mkl_blacs_intelmpi_lp64.dll
167592 INFO: Looking for eggs
167592 INFO: Using Python library d:\users\tianya\anaconda3\python38.dll
167593 INFO: Found binding redirects:
[]
167612 INFO: Warnings written to E:\fish AI\py--exe-测试\build\OpenCV-Python-faces\warn-OpenCV-Python-faces.txt
167908 INFO: Graph cross-reference written to E:\fish AI\py--exe-测试\build\OpenCV-Python-faces\xref-OpenCV-Python-faces.html
168024 INFO: checking PYZ
168024 INFO: Building PYZ because PYZ-00.toc is non existent
168025 INFO: Building PYZ (ZlibArchive) E:\fish AI\py--exe-测试\build\OpenCV-Python-faces\PYZ-00.pyz
171164 INFO: Building PYZ (ZlibArchive) E:\fish AI\py--exe-测试\build\OpenCV-Python-faces\PYZ-00.pyz completed successfully.
171223 INFO: checking PKG
171224 INFO: Building PKG because PKG-00.toc is non existent
171224 INFO: Building PKG (CArchive) PKG-00.pkg
316592 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
316646 INFO: Bootloader d:\users\tianya\anaconda3\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
316646 INFO: checking EXE
316646 INFO: Building EXE because EXE-00.toc is non existent
316647 INFO: Building EXE from EXE-00.toc
316647 INFO: Appending archive to EXE E:\fish AI\py--exe-测试\dist\OpenCV-Python-faces.exe
317460 INFO: Building EXE from EXE-00.toc completed successfully.

可以看见 在编译的过程中提示了很多错误信息,(先不管他)。但是后面提示还是编译成功。

我们现在去查看目录:

Windows环境使用 PyInstaller 打包python .py文件 安装和使用教程(保姆级别-小白版)_第1张图片

进入dist 目录

Windows环境使用 PyInstaller 打包python .py文件 安装和使用教程(保姆级别-小白版)_第2张图片

 

可以看见在此生成了OpenCV-Python-faces.exe 文件,这就是我们需要的结果了。

(好多文章描述到这里就结束了。)

我们继续:

双击打开OpenCV-Python-faces.exe 文件,就像我们执行其他Windows程序一样。

你会看见弹出来CMD命令界面,

Windows环境使用 PyInstaller 打包python .py文件 安装和使用教程(保姆级别-小白版)_第3张图片

停留一会,然后就关闭了。

------------问题出现了,怎么回事呢?

分析如下:

1.首先文件打包是成功了。

2.CMD执行界面已经打开,但是过一会就退出了。(是不是它在等待什么指令呢)

好,现在我们回到我们的python程序分析,我们做的是简单的opencv人脸识别程序。

识别原理如下:

利用opencv打开摄像头----捕获视频--转为图片---识别图片--比对人脸--画出人脸--展示结果。

问题出现在比对环节。

我们没有比对的数据。

好,我们现在复制opecv官方的提供的人脸和眼睛数据到目录下:

haarcascade_eye.xml

haarcascade_frontalface_default.xml

复制完成:

Windows环境使用 PyInstaller 打包python .py文件 安装和使用教程(保姆级别-小白版)_第4张图片

OK ,现在我们再次执行

结果如下:

Windows环境使用 PyInstaller 打包python .py文件 安装和使用教程(保姆级别-小白版)_第5张图片

 

完美,(请无视本人照片▲)

参考链接:https://pyinstaller.readthedocs.io/en/stable/usage.html

你可能感兴趣的:(OpenCV,pyinstaller)