工具:Pycharm2019.2.2 + Anaconda3(导入需要的库及Python.exe以及pyinstaller)
在PyCharm中点击工具菜单栏File后点击Settings后按如下图所示步骤进行:
开始下载…
下载成功(右下角会弹出提示框!)-*-
成功后开始进入正题:打包.exe运行文件
a.图标下载网站链接: 图标下载网站,也可以自己准备或设计好的图片,
b.然后通过图标转换网站链接: 图标转换网站转换为合适可用的图标。
备注:所有图片路径必须改为绝对路径(否则无法显示)如:
self.setWindowIcon(QIcon(‘D:/工程名/systemcode/images/icon1.png’)) # 设置窗体标题图标
1.在控制台中选择Terminal
输入形如以下形式内容(格式如:pyinstaller -F D:\工程名\systemcode\main.py):
运行过程如下:
(base) D:\工程名>pyinstaller -F D:\工程名\systemcode\main.py
224 INFO: PyInstaller: 3.6
225 INFO: Python: 3.7.3 (conda)
227 INFO: Platform: Windows-10-10.0.18362-SP0
229 INFO: wrote D:\工程名\main.spec
237 INFO: UPX is not available.
248 INFO: Extending PYTHONPATH with paths
['D:\\工程名', 'D:\\工程名']
248 INFO: checking Analysis
249 INFO: Building Analysis because Analysis-00.toc is non existent
249 INFO: Initializing module dependency graph...
270 INFO: Caching module graph hooks...
288 INFO: Analyzing base_library.zip ...
16899 INFO: Caching module dependency graph...
17181 INFO: running Analysis Analysis-00.toc
17244 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by D:\Anaconda3\python.exe
20606 INFO: Analyzing D:\工程名\systemcode\main.py
22940 INFO: Processing pre-find module path hook distutils
22975 INFO: distutils: retargeting to non-venv dir 'D:\\Anaconda3\\lib'
24081 INFO: Processing module hooks...
24082 INFO: Loading module hook "hook-cv2.py"...
39939 INFO: Processing pre-safe import module hook setuptools.extern.six.moves
42892 INFO: Processing pre-find module path hook site
42900 INFO: site: retargeting to fake-dir 'D:\\Anaconda3\\lib\\site-packages\\PyInstaller\\fake-modules'
66116 INFO: Loading module hook "hook-distutils.py"...
66148 INFO: Loading module hook "hook-encodings.py"...
67143 INFO: Loading module hook "hook-importlib_metadata.py"...
67149 INFO: Loading module hook "hook-lib2to3.py"...
67160 INFO: Loading module hook "hook-numpy.core.py"...
68459 INFO: MKL libraries found when importing numpy. Adding MKL to binaries
68482 INFO: Loading module hook "hook-numpy.py"...
68486 INFO: Loading module hook "hook-pkg_resources.py"...
71269 INFO: Processing pre-safe import module hook win32com
72539 INFO: Excluding import '__main__'
72541 INFO: Removing import of __main__ from module pkg_resources
72541 INFO: Loading module hook "hook-pycparser.py"...
72543 INFO: Loading module hook "hook-pydoc.py"...
72545 INFO: Loading module hook "hook-PyQt5.py"...
73014 INFO: Loading module hook "hook-PyQt5.QtCore.py"...
73283 INFO: Loading module hook "hook-PyQt5.QtGui.py"...
73747 INFO: Loading module hook "hook-PyQt5.QtWidgets.py"...
74148 INFO: Loading module hook "hook-pytest.py"...
80434 INFO: Loading module hook "hook-pythoncom.py"...
82638 INFO: Loading module hook "hook-pywintypes.py"...
84663 INFO: Loading module hook "hook-scipy.py"...
84701 INFO: Loading module hook "hook-setuptools.py"...
88301 INFO: Loading module hook "hook-sysconfig.py"...
88359 INFO: Loading module hook "hook-win32com.py"...
90940 INFO: Loading module hook "hook-xml.dom.domreg.py"...
90942 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...
90944 INFO: Loading module hook "hook-xml.py"...
91005 INFO: Looking for ctypes DLLs
91213 INFO: Analyzing run-time hooks ...
91222 INFO: Including run-time hook 'pyi_rth_pyqt5.py'
91243 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
91260 INFO: Including run-time hook 'pyi_rth_pkgres.py'
91282 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py'
91336 INFO: Looking for dynamic libraries
91478 WARNING: lib not found: msmpi.dll dependency of D:\Anaconda3\Library\bin\mkl_blacs_msmpi_ilp64.dll
91541 WARNING: lib not found: mpich2mpi.dll dependency of D:\Anaconda3\Library\bin\mkl_blacs_mpich2_lp64.dll
92243 WARNING: lib not found: mpich2mpi.dll dependency of D:\Anaconda3\Library\bin\mkl_blacs_mpich2_ilp64.dll
93386 WARNING: lib not found: impi.dll dependency of D:\Anaconda3\Library\bin\mkl_blacs_intelmpi_lp64.dll
94157 WARNING: lib not found: pgf90rtl.dll dependency of D:\Anaconda3\Library\bin\mkl_pgi_thread.dll
94167 WARNING: lib not found: pgc14.dll dependency of D:\Anaconda3\Library\bin\mkl_pgi_thread.dll
94177 WARNING: lib not found: pgf90.dll dependency of D:\Anaconda3\Library\bin\mkl_pgi_thread.dll
96204 WARNING: lib not found: impi.dll dependency of D:\Anaconda3\Library\bin\mkl_blacs_intelmpi_ilp64.dll
97152 WARNING: lib not found: msmpi.dll dependency of D:\Anaconda3\Library\bin\mkl_blacs_msmpi_lp64.dll
101534 WARNING: lib not found: api-ms-win-downlevel-shlwapi-l1-1-0.dll dependency of D:\Anaconda3\Library\bin\opencv_videoio342.dll
102988 INFO: Looking for eggs
102988 INFO: Using Python library D:\Anaconda3\python37.dll
102989 INFO: Found binding redirects:
[]
103009 INFO: Warnings written to D:\工程名\build\main\warn-main.txt
103213 INFO: Graph cross-reference written to D:\工程名\build\main\xref-main.html
103297 INFO: checking PYZ
103298 INFO: Building PYZ because PYZ-00.toc is non existent
103298 INFO: Building PYZ (ZlibArchive) D:\工程名\build\main\PYZ-00.pyz
105361 INFO: Building PYZ (ZlibArchive) D:\工程名\build\main\PYZ-00.pyz completed successfully.
105453 INFO: checking PKG
105453 INFO: Building PKG because PKG-00.toc is non existent
105454 INFO: Building PKG (CArchive) PKG-00.pkg
285478 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
285516 INFO: Bootloader D:\Anaconda3\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
285518 INFO: checking EXE
285534 INFO: Building EXE because EXE-00.toc is non existent
285535 INFO: Building EXE from EXE-00.toc
285548 INFO: Appending archive to EXE D:\工程名\dist\main.exe
286886 INFO: Building EXE from EXE-00.toc completed successfully.
注意在运行过程中有很多WARNNING:
我们需要把这些都找出来放到一个新文件夹中如下所示:
2.修改生成的main.spec文件,在其中修改位置做出修改如:
hiddenimports=[‘MainUi.py’, ‘SecondUi’, ‘c57.py’],
console=False , icon=‘D:\工程名\systemcode\logo.ico’
如下图所示:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['D:\\工程名\\systemcode\\main.py'],
pathex=['D:\\工程名'],
binaries=[],
datas=[],
hiddenimports=['MainUi.py', 'SecondUi', 'c57.py'], #修改位置:添加其他.py文件
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False , icon='D:\\工程名\\systemcode\\logo.ico' ) #修改位置:添加图标位置
当我们修改完main.spec之后,对文件进行打包了,在命令框中输入如下命令
(形如:pyinstaller -F -w -i D:\工程名\systemcode\logo.ico main.spec):
点击运行过程如下:
(base) D:\工程名>pyinstaller -F -w -i D:\project\systemcode\logo.ico main.spec
231 INFO: PyInstaller: 3.6
232 INFO: Python: 3.7.3 (conda)
233 INFO: Platform: Windows-10-10.0.18362-SP0
240 INFO: UPX is not available.
243 INFO: Extending PYTHONPATH with paths
['D:\\工程名', 'D:\\工程名']
243 INFO: checking Analysis
256 INFO: Building because hiddenimports changed
256 INFO: Initializing module dependency graph...
276 INFO: Caching module graph hooks...
286 INFO: Analyzing base_library.zip ...
13046 INFO: Caching module dependency graph...
13322 INFO: running Analysis Analysis-00.toc
13353 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by D:\Anaconda3\python.exe
14102 INFO: Analyzing D:\工程名\systemcode\main.py
15118 INFO: Processing pre-find module path hook distutils
15120 INFO: distutils: retargeting to non-venv dir 'D:\\Anaconda3\\lib'
15864 INFO: Analyzing hidden import 'MainUi.py'
15868 ERROR: Hidden import 'MainUi.py' not found
15868 INFO: Analyzing hidden import 'SecondUi'
15872 ERROR: Hidden import 'SecondUi' not found
15872 INFO: Analyzing hidden import 'c57.py'
15879 ERROR: Hidden import 'c57.py' not found
15879 INFO: Processing module hooks...
15880 INFO: Loading module hook "hook-cv2.py"...
29174 INFO: Processing pre-safe import module hook setuptools.extern.six.moves
32195 INFO: Processing pre-find module path hook site
32197 INFO: site: retargeting to fake-dir 'D:\\Anaconda3\\lib\\site-packages\\PyInstaller\\fake-modules'
50105 INFO: Loading module hook "hook-distutils.py"...
50108 INFO: Loading module hook "hook-encodings.py"...
50502 INFO: Loading module hook "hook-importlib_metadata.py"...
50508 INFO: Loading module hook "hook-lib2to3.py"...
50518 INFO: Loading module hook "hook-numpy.core.py"...
50826 INFO: MKL libraries found when importing numpy. Adding MKL to binaries
50847 INFO: Loading module hook "hook-numpy.py"...
50849 INFO: Loading module hook "hook-pkg_resources.py"...
53673 INFO: Processing pre-safe import module hook win32com
54626 INFO: Excluding import '__main__'
54628 INFO: Removing import of __main__ from module pkg_resources
54629 INFO: Loading module hook "hook-pycparser.py"...
54630 INFO: Loading module hook "hook-pydoc.py"...
54632 INFO: Loading module hook "hook-PyQt5.py"...
55169 INFO: Loading module hook "hook-PyQt5.QtCore.py"...
55386 INFO: Loading module hook "hook-PyQt5.QtGui.py"...
55830 INFO: Loading module hook "hook-PyQt5.QtWidgets.py"...
56219 INFO: Loading module hook "hook-pytest.py"...
60422 INFO: Loading module hook "hook-pythoncom.py"...
62380 INFO: Loading module hook "hook-pywintypes.py"...
64671 INFO: Loading module hook "hook-scipy.py"...
64679 INFO: Loading module hook "hook-setuptools.py"...
67959 INFO: Loading module hook "hook-sysconfig.py"...
67962 INFO: Loading module hook "hook-win32com.py"...
70088 INFO: Loading module hook "hook-xml.dom.domreg.py"...
70090 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...
70092 INFO: Loading module hook "hook-xml.py"...
70166 INFO: Looking for ctypes DLLs
70414 INFO: Analyzing run-time hooks ...
70424 INFO: Including run-time hook 'pyi_rth_pyqt5.py'
70426 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
70433 INFO: Including run-time hook 'pyi_rth_pkgres.py'
70440 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py'
70474 INFO: Looking for dynamic libraries
70552 WARNING: lib not found: msmpi.dll dependency of D:\Anaconda3\Library\bin\mkl_blacs_msmpi_ilp64.dll
70770 WARNING: lib not found: mpich2mpi.dll dependency of D:\Anaconda3\Library\bin\mkl_blacs_mpich2_ilp64.dll
71062 WARNING: lib not found: impi.dll dependency of D:\Anaconda3\Library\bin\mkl_blacs_intelmpi_ilp64.dll
71619 WARNING: lib not found: pgc14.dll dependency of D:\Anaconda3\Library\bin\mkl_pgi_thread.dll
71628 WARNING: lib not found: pgf90rtl.dll dependency of D:\Anaconda3\Library\bin\mkl_pgi_thread.dll
71637 WARNING: lib not found: pgf90.dll dependency of D:\Anaconda3\Library\bin\mkl_pgi_thread.dll
72151 WARNING: lib not found: msmpi.dll dependency of D:\Anaconda3\Library\bin\mkl_blacs_msmpi_lp64.dll
72204 WARNING: lib not found: impi.dll dependency of D:\Anaconda3\Library\bin\mkl_blacs_intelmpi_lp64.dll
72244 WARNING: lib not found: mpich2mpi.dll dependency of D:\Anaconda3\Library\bin\mkl_blacs_mpich2_lp64.dll
74930 WARNING: lib not found: api-ms-win-downlevel-shlwapi-l1-1-0.dll dependency of D:\Anaconda3\Library\bin\opencv_videoio342.dll
75978 INFO: Looking for eggs
75978 INFO: Using Python library D:\Anaconda3\python37.dll
75979 INFO: Found binding redirects:
[]
76044 INFO: Warnings written to D:\工程名\build\main\warn-main.txt
76265 INFO: Graph cross-reference written to D:\工程名\build\main\xref-main.html
76316 INFO: checking PYZ
76329 INFO: Building because toc changed
76329 INFO: Building PYZ (ZlibArchive) D:\工程名\build\main\PYZ-00.pyz
79360 INFO: Building PYZ (ZlibArchive) D:\工程名\build\main\PYZ-00.pyz completed successfully.
79392 INFO: checking PKG
79397 INFO: Building because toc changed
79397 INFO: Building PKG (CArchive) PKG-00.pkg
228706 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
228720 INFO: Bootloader D:\Anaconda3\lib\site-packages\PyInstaller\bootloader\Windows-64bit\runw.exe
228720 INFO: checking EXE
228727 INFO: Building because console changed
228727 INFO: Building EXE from EXE-00.toc
228840 INFO: Copying icons from ['D:\\工程名\\systemcode\\logo.ico']
228924 INFO: Writing RT_GROUP_ICON 0 resource with 20 bytes
228924 INFO: Writing RT_ICON 1 resource with 4264 bytes
228935 INFO: Updating manifest in D:\工程名\build\main\runw.exe.t4_sreaw
229409 INFO: Updating resource type 24 name 1 language 0
229415 INFO: Appending archive to EXE D:\工程名\dist\main.exe
230034 INFO: Building EXE from EXE-00.toc completed successfully.
打包成功!!!
在生成的dist文件夹中生成了打包好的.exe文件如下图所示:
点击main.exe,正常运行。
你以为这就完了(当时我也是怎么以为),此时我们需要把生成的main.exe文件放入刚才新建的文件夹中如下图所示:
然后关掉你的PyCharm,如果可以正常运行,这样就真的可以了。
END