欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

只需三步即可将 Python 程序转换成 exe 文件

程序员文章站 2022-07-07 09:08:47
...

只需三步即可将 Python 程序转换成 exe 文件

用pyinstaller可以将.py文件转换成.exe格式,让你的程序在没有安装python环境的windows上运行。

一、在cmd命令行执行安装pyinstaller工具

pip install pyinstaller

二、下载安装pyinstaler运行时所需windows扩展:pywin32

mhammond/pywin32
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

四、打包结果如下:

只需三步即可将 Python 程序转换成 exe 文件

五、运行打包好的程序

只需三步即可将 Python 程序转换成 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.exe
Traceback (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_pkgres

C:\Users\www_9>
  • 解决办法(卸载高版本setuptools ,然后安装setuptools44.0):
  1. 卸载高版本:pip uninstall setuptools
(venv) C:\Users\www_9\PycharmProjects\python-tanchishe>pip uninstall setuptools
Found existing installation: setuptools 46.1.3
Uninstalling 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.exe
Proceed (y/n)? y
  Successfully uninstalled setuptools-46.1.3
  1. 安装低版本: pip install setuptools==44.0.0
(venv) C:\Users\www_9\PycharmProjects\python-tanchishe>pip install setuptools==44.0.0
Collecting setuptools==44.0.0
  Downloading setuptools-44.0.0-py2.py3-none-any.whl (583 kB)
     |████████████████████████████████| 583 kB 26 kB/s
Installing collected packages: setuptools
Successfully installed setuptools-44.0.0
  1. 重新打包程序:>pyinstaller -F tanchishe.py
(venv) C:\Users\www_9\PycharmProjects\python-tanchishe>pyinstaller -F tanchishe.py
97 INFO: PyInstaller: 3.6
98 INFO: Python: 3.7.5
98 INFO: Platform: Windows-10-10.0.18362-SP0
100 INFO: wrote C:\Users\www_9\PycharmProjects\python-tanchishe\tanchishe.spec
118 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 Analysis
125 INFO: Building because c:\users\www_9\pycharmprojects\python-tanchishe\venv\lib\site-packages\pkg_resources\__init__.py changed
125 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.toc
3800 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\users\www_9\pycharmprojects\python-tanchishe\venv\scripts\python.exe
3824 INFO: Analyzing C:\Users\www_9\PycharmProjects\python-tanchishe\tanchishe.py
4200 INFO: Processing pre-find module path hook   distutils
4201 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   site
4265 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   win32com
Traceback (most recent call last):
  File "<string>", line 2, in <module>
ModuleNotFoundError: No module named 'win32com'
6464 INFO: Processing pre-safe import module hook   win32com
Traceback (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_resources
6792 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 DLLs
6842 INFO: Analyzing run-time hooks ...
6848 INFO: Including run-time hook 'pyi_rth_pkgres.py'
6853 INFO: Looking for dynamic libraries
8980 INFO: Looking for eggs
8980 INFO: Using Python library C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python37.dll
8981 INFO: Found binding redirects:
[]
8985 INFO: Warnings written to C:\Users\www_9\PycharmProjects\python-tanchishe\build\tanchishe\warn-tanchishe.txt
9035 INFO: Graph cross-reference written to C:\Users\www_9\PycharmProjects\python-tanchishe\build\tanchishe\xref-tanchishe.html
9054 INFO: checking PYZ
9056 INFO: Building because toc changed
9057 INFO: Building PYZ (ZlibArchive) C:\Users\www_9\PycharmProjects\python-tanchishe\build\tanchishe\PYZ-00.pyz
9922 INFO: Building PYZ (ZlibArchive) C:\Users\www_9\PycharmProjects\python-tanchishe\build\tanchishe\PYZ-00.pyz completed successfully.
9943 INFO: checking PKG
9946 INFO: Building because toc changed
9946 INFO: Building PKG (CArchive) PKG-00.pkg
12773 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.exe
12780 INFO: checking EXE
12783 INFO: Building because toc changed
12783 INFO: Building EXE from EXE-00.toc
12786 INFO: Appending archive to EXE C:\Users\www_9\PycharmProjects\python-tanchishe\dist\tanchishe.exe
12796 INFO: Building EXE from EXE-00.toc completed successfully.

(venv) C:\Users\www_9\PycharmProjects\python-tanchishe>
  1. 然后运行编译所生成的exe文件
C:\Users\www_9>C:\Users\www_9\PycharmProjects\python-tanchishe\dist\tanchishe.exe
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html

C:\Users\www_9>
  1. tanchishe.exe可以正常运行了
    只需三步即可将 Python 程序转换成 exe 文件
  • 可参考资料:搜索收集的相关知识页面,这里记录下来,方便日后参考:
打包教程:
	https://zhuanlan.zhihu.com/p/38659588

Pyinstaller No module named pkg_resources.py2_warn:
	https://blog.51cto.com/cfy10/2468597?source=dra
	
pyInstaller打包完成以后运行程序时出现ModuleNotFoundError: No module named 'pkg_resources.py2_warn:
	https://blog.csdn.net/BINGYiii/article/details/104193180
	
windows下安装setuptools与pip:
	https://www.cnblogs.com/longronglang/p/10443625.html
	
下载不同版本 settools  不同版本地址:
	https://pypi.Python.org/pypi/setuptools
	
setuptools 44.0版本地址:
	https://pypi.org/project/setuptools/44.0.0/