Only three steps can convert Python program into exe file

Only three steps can convert Python program into exe file

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

1. Install the pyinstaller tool on the cmd command line

pip install pyinstaller

2. Download and install the windows extension required for pyinstaler runtime: pywin32

mhammond/pywin32
https://github.com/mhammond/pywin32/releases
Select the latest version to download, pay attention to select the corresponding python version and bit number,
such as 32-bit python3.7.4 corresponding download: [pywin32-223.win32-py3. 7.exe]
such as 64-bit python3.7.4 corresponding download: [pywin32-223.win-amd64-py3.7.exe]

Three, execute the packaging command in the command line

格式如下:
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

Here we execute and start packaging:

(venv) C:\Users\www_9\PycharmProjects\python-tanchishe>pyinstaller -F tanchishe.py

4. The packaging results are as follows:

Insert picture description here

Five, run the packaged program

Insert picture description here

6. Error handling:

  • The execution error of the exe file packaged by Pyinstaller is as follows (the Python version I use is: 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>
  • Solution (uninstall the high version of setuptools, then install setuptools44.0):
  1. Uninstall the high version: 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. Install the lower version: 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. Repackage program:>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. Then run the compiled exe file
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 can run normally
    Insert picture description here
  • Reference materials: search the relevant knowledge pages collected, record them here for future reference:
打包教程:
	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/

Guess you like

Origin blog.csdn.net/one312/article/details/105736261