pip install pyinstaller
https://github.com/mhammond/pywin32/releases选择最新版的下载,注意选择与python版本和位数对应的如32位python3.7.4对应电脑 下载:【pywin32-223.win32-py3.7.exe】如64位python3.7.4对应下载:【pywin32-223.win-amd64-py3.7.exe】
三、在命令行中执行打包命令即可
格式如下:pyinstaller [opts] yourprogram.py参数含义-F 指定打包后只生成一个exe格式的文件(建议写上这个参数)-D –onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)-c –console, –nowindowed 使用控制台,无界面(默认)-w –windowed, –noconsole 使用窗口,无控制台-p 添加搜索路径,让其找到对应的库。-i 改变生成程序的icon图标例1.指定文件路径打包:pyinstaller -F D:\project\tanchishe.py例2.不带窗口,后台运行打包:pyinstaller -F -w D:\project\tanchishe.py例3.更换程序图标打包:pyinstaller -F -w -i D:\project\test.ico D:\project\tanchishe.py
这里咱们执行开始打包:(venv) C:\Users\www_9\PycharmProjects\python-tanchishe>pyinstaller -F tanchishe.py
四、打包结果如下:
五、运行打包好的exe文件tanchishe.exe效果如下:
六、报错处理:
执行Pyinstaller 打包的exe文件执行报错如下(我用的Python版本为:python3.7.5):Microsoft Windows [版本 10.0.18363.778](c) 2019 Microsoft Corporation。保留所有权利。C:\Users\www_9>C:\Users\www_9\PycharmProjects\python-tanchishe\dist\tanchishe.exeTraceback (most recent call last): File "lib\site-packages\PyInstaller\loader\rthooks\pyi_rth_pkgres.py", line 13, in <module> File "c:\users\www_9\pycharmprojects\python-tanchishe\venv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module exec(bytecode, module.__dict__) File "lib\site-packages\pkg_resources\__init__.py", line 86, in <module>ModuleNotFoundError: No module named 'pkg_resources.py2_warn'[30844] Failed to execute script pyi_rth_pkgresC:\Users\www_9>
解决办法(卸载高版本setuptools ,然后安装setuptools44.0):卸载高版本:pip uninstall setuptools
(venv) C:\Users\www_9\PycharmProjects\python-tanchishe>pip uninstall setuptoolsFound existing installation: setuptools 46.1.3Uninstalling setuptools-46.1.3: Would remove: c:\users\www_9\pycharmprojects\python-tanchishe\venv\lib\site-packages\easy_install.py c:\users\www_9\pycharmprojects\python-tanchishe\venv\lib\site-packages\pkg_resources\* c:\users\www_9\pycharmprojects\python-tanchishe\venv\lib\site-packages\setuptools-46.1.3.dist-info\* c:\users\www_9\pycharmprojects\python-tanchishe\venv\lib\site-packages\setuptools\* c:\users\www_9\pycharmprojects\python-tanchishe\venv\scripts\easy_install-3.7.exe c:\users\www_9\pycharmprojects\python-tanchishe\venv\scripts\easy_install.exeProceed (y/n)? y Successfully uninstalled setuptools-46.1.3
安装低版本: pip install setuptools==44.0.0
(venv) C:\Users\www_9\PycharmProjects\python-tanchishe>pip install setuptools==44.0.0Collecting setuptools==44.0.0 Downloading setuptools-44.0.0-py2.py3-none-any.whl (583 kB) |████████████████████████████████| 583 kB 26 kB/sInstalling collected packages: setuptoolsSuccessfully installed setuptools-44.0.0
重新打包程序:>pyinstaller -F tanchishe.py
(venv) C:\Users\www_9\PycharmProjects\python-tanchishe>pyinstaller -F tanchishe.py97 INFO: PyInstaller: 3.698 INFO: Python: 3.7.598 INFO: Platform: Windows-10-10.0.18362-SP0100 INFO: wrote C:\Users\www_9\PycharmProjects\python-tanchishe\tanchishe.spec118 INFO: UPX is not available.120 INFO: Extending PYTHONPATH with paths['C:\\Users\\www_9\\PycharmProjects\\python-tanchishe', 'C:\\Users\\www_9\\PycharmProjects\\python-tanchishe']120 INFO: checking Analysis125 INFO: Building because c:\users\www_9\pycharmprojects\python-tanchishe\venv\lib\site-packages\pkg_resources\__init__.py changed125 INFO: Initializing module dependency graph...131 INFO: Caching module graph hooks...138 INFO: Analyzing base_library.zip ...3643 INFO: Caching module dependency graph...3794 INFO: running Analysis Analysis-00.toc3800 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable required by c:\users\www_9\pycharmprojects\python-tanchishe\venv\scripts\python.exe3824 INFO: Analyzing C:\Users\www_9\PycharmProjects\python-tanchishe\tanchishe.py4200 INFO: Processing pre-find module path hook distutils4201 INFO: distutils: retargeting to non-venv dir 'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python37_64\\lib'4264 INFO: Processing pre-find module path hook site4265 INFO: site: retargeting to fake-dir 'c:\\users\\www_9\\pycharmprojects\\python-tanchishe\\venv\\lib\\site-packages\\PyInstaller\\fake-modules'5648 INFO: Processing module hooks...5648 INFO: Loading module hook "hook-distutils.py"...5651 INFO: Loading module hook "hook-encodings.py"...5860 INFO: Loading module hook "hook-pkg_resources.py"...6338 INFO: Processing pre-safe import module hook 电脑 win32comTraceback (most recent call last): File "<string>", line 2, in <module>ModuleNotFoundError: No module named 'win32com'6464 INFO: Processing pre-safe import module hook win32comTraceback (most recent call last): File "<string>", line 2, in <module>ModuleNotFoundError: No module named 'win32com'6790 INFO: Excluding import '__main__'6792 INFO: Removing import of __main__ from module pkg_resources6792 INFO: Loading module hook "hook-pydoc.py"...6794 INFO: Loading module hook "hook-pygame.py"...6795 WARNING: Hidden import "pygame._view" not found!6795 INFO: Loading module hook "hook-sysconfig.py"...6798 INFO: Loading module hook "hook-xml.dom.domreg.py"...6799 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...6800 INFO: Loading module hook 电脑"hook-xml.py"...6827 INFO: Looking for ctypes DLLs6842 INFO: Analyzing run-time hooks ...6848 INFO: Including run-time hook 'pyi_rth_pkgres.py'6853 INFO: Looking for dynamic libraries8980 INFO: Looking for eggs8980 INFO: Using Python library C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python37.dll8981 INFO: Found binding redirects:[]8985 INFO: Warnings written to C:\Users\www_9\PycharmProjects\python-tanchishe\build\tanchishe\warn-tanchishe.txt9035 INFO: Graph cross-reference written to C:\Users\www_9\PycharmProjects\python-tanchishe\build\tanchishe\xref-tanchishe.html9054 INFO: checking PYZ9056 INFO: Building because toc changed9057 INFO: Building PYZ (ZlibArchive) C:\Users\www_9\PycharmProjects\python-tanchishe\build\tanchishe\PYZ-00.pyz9922 INFO: Building PYZ (ZlibArchive) C:\Users\www_9\PycharmProjects\python-tanchishe\build\tanchishe\PYZ-00.电脑pyz completed successfully.9943 INFO: checking PKG9946 INFO: Building because toc changed9946 INFO: Building PKG (CArchive) PKG-00.pkg12773 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.12779 INFO: Bootloader c:\users\www_9\pycharmprojects\python-tanchishe\venv\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe12780 INFO: checking EXE12783 INFO: Building because toc changed12783 INFO: Building EXE from EXE-00.toc12786 INFO: Appending archive to EXE C:\Users\www_9\PycharmProjects\python-tanchishe\dist\tanchishe.exe12796 INFO: Building EXE from EXE-00.toc completed successfully.(venv) C:\Users\www_9\PycharmProjects\python-tanchishe>
然后运行编译所生成的exe文件
C:\Users\www_9>C:\Users\www_9\PycharmProjects\python-tanchishe\dist\tanchishe.exepygame 1.9.6Hello from the pygame community. https://www.pygame.org/contribute.htmlC:\Users\www_9>
tanchishe.exe可以正常运行了
用pyinstaller可以将.py文件转换成.exe格式,让你的程序在没有安装python环境的windows上运行:您也可以参考这里:https://zhuanlan.zhihu.com/p/38659588报错解决参考地址:Pyinstaller No module named pkg_resources.py2_warn:https://blog.51cto.com/cfy10/2468597?source=drapyInstaller打包完成以后运行程序时出现ModuleNotFoundError: No module named 'pkg_resources.py2_warn:https://blog.csdn.net/BINGYiii/article/details/104193180windows下安装setuptools与pip:https://www.cnblogs.com/longronglang/p/10443625.html下载不同版本 settools 不同版本地址:https://pypi.Python.org/pypi/setuptoolssetuptools 44.0版本地址:https://pypi.org/project/setuptools/44.0.0/
电脑