【Python-GUI开发】六 将py文件转换为exe可执行文件

1、安装pyinstaller

pip install pyinstaller

打开cmd,输入pyinstaller --version

若能正常显示pyinstaller版本号即表示安装成功。

2、将py文件转换为exe文件

在xxx.py文件所在目录下,按住shift+右击,选择“在此处打开powershell窗口”,输入

pyinstaller -F hello.py

3、pyinstaller生成.exe文件跳过命令行窗口,不弹出命令窗口

pyinstaller打包.py文件后,双击打开生成的.exe文件,会先跳出命令行窗口,回车之后才开始运行。可以通过增加选项的方式跳过命令行,直接打开exe:
-D, --onedir 创建一个目录,包含exe文件和依赖文件(默认)
-F, --onefile 创建一个exe文件,所有依赖文件都打包进这个exe文件
-c, --console, --nowindowed 有控制台,无界面 (默认)
-w, --windowed, --noconsole 有窗口,无控制台
只需将:pyinstaller -F xxx.py
改为: pyinstaller -w -F xxx.py

pyinstaller -F -w hello.py

4、给exe程序添加图标

需要准备一张.ico后缀名的图片(可自行网上下载或者用png图片在线生成ico图标)
然后使用命令(注意路径)


pyinstaller -F -w --icon=.\Src\tools.ico ThinkHome_Tools.py

  • 最后需要注意的是,需要移动exe程序的位置,才可以显示图片

 

异常报错

发现执行后出现报错: 

PS E:\01_政企产品线\04_个人成长\02_python> pyinstaller -F .\hello.py
65 INFO: PyInstaller: 3.5
65 INFO: Python: 3.8.0
66 INFO: Platform: Windows-10-10.0.17763-SP0
83 INFO: wrote E:\01_政企产品线\04_个人成长\02_python\hello.spec
85 INFO: UPX is not available.
96 INFO: Extending PYTHONPATH with paths
['E:\\01_政企产品线\\04_个人成长\\02_python', 'E:\\01_政企产品线\\04_个人成长\\02_python']
96 INFO: checking Analysis
97 INFO: Building Analysis because Analysis-00.toc is non existent
97 INFO: Initializing module dependency graph...
99 INFO: Initializing module graph hooks...
103 INFO: Analyzing base_library.zip ...
3304 INFO: running Analysis Analysis-00.toc
3308 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\users\39558\appdata\local\programs\python\python38\python.exe
4061 INFO: Caching module hooks...
4105 INFO: Analyzing E:\01_政企产品线\04_个人成长\02_python\hello.py
4108 INFO: Loading module hooks...
4108 INFO: Loading module hook "hook-encodings.py"...
4174 INFO: Loading module hook "hook-pydoc.py"...
4176 INFO: Loading module hook "hook-xml.py"...
4352 INFO: Looking for ctypes DLLs
4354 INFO: Analyzing run-time hooks ...
4356 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
4363 INFO: Looking for dynamic libraries
6194 INFO: Looking for eggs
6194 INFO: Using Python library c:\users\39558\appdata\local\programs\python\python38\python38.dll
6194 INFO: Found binding redirects:
[]
6227 INFO: Warnings written to E:\01_政企产品线\04_个人成长\02_python\build\hello\warn-hello.txt
6264 INFO: Graph cross-reference written to E:\01_政企产品线\04_个人成长\02_python\build\hello\xref-hello.html
6273 INFO: checking PYZ
6273 INFO: Building PYZ because PYZ-00.toc is non existent
6273 INFO: Building PYZ (ZlibArchive) E:\01_政企产品线\04_个人成长\02_python\build\hello\PYZ-00.pyz
Traceback (most recent call last):
  File "c:\users\39558\appdata\local\programs\python\python38\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\39558\appdata\local\programs\python\python38\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\39558\AppData\Local\Programs\Python\Python38\Scripts\pyinstaller.exe\__main__.py", line 9, in 
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\__main__.py", line 111, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\build_main.py", line 844, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\build_main.py", line 791, in build
    exec(code, spec_namespace)
  File "E:\01_政企产品线\04_个人成长\02_python\hello.spec", line 18, in 
    pyz = PYZ(a.pure, a.zipped_data,
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\api.py", line 98, in __init__
    self.__postinit__()
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\datastruct.py", line 158, in __postinit__
    self.assemble()
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\api.py", line 128, in assemble
    self.code_dict = {
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\api.py", line 129, in 
    key: strip_paths_in_code(code)
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\utils.py", line 652, in strip_paths_in_code
    consts = tuple(
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\utils.py", line 653, in 
    strip_paths_in_code(const_co, new_filename)
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\utils.py", line 652, in strip_paths_in_code
    consts = tuple(
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\utils.py", line 653, in 
    strip_paths_in_code(const_co, new_filename)
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\utils.py", line 660, in strip_paths_in_code
    return code_func(co.co_argcount, co.co_kwonlyargcount, co.co_nlocals, co.co_stacksize,
TypeError: an integer is required (got type bytes)

解决办法:报错前的pyinstaller是用 pip install pyinstaller 来安装的,

改成用 pip install https://github.com/pyinstaller/pyinstaller/archive/develop.tar.gz 再安装一次

原因:可能是电脑中同时存在多个版本的python会导致的问题

再次执行

pyinstaller -D hello.py

pyinstaller支持的选项如下所示,更多的可以pyinstaller -h查看 

【Python-GUI开发】六 将py文件转换为exe可执行文件_第1张图片

PS E:\01_政企产品线\04_个人成长\02_python> pyinstaller -D hello.py
62 INFO: PyInstaller: 3.5
63 INFO: Python: 3.8.0
65 INFO: Platform: Windows-10-10.0.17763-SP0
68 INFO: wrote E:\01_政企产品线\04_个人成长\02_python\hello.spec
70 INFO: UPX is not available.
72 INFO: Extending PYTHONPATH with paths
['E:\\01_政企产品线\\04_个人成长\\02_python', 'E:\\01_政企产品线\\04_个人成长\\02_python']
72 INFO: checking Analysis
87 INFO: checking PYZ
88 INFO: Building PYZ because PYZ-00.toc is non existent
88 INFO: Building PYZ (ZlibArchive) E:\01_政企产品线\04_个人成长\02_python\build\hello\PYZ-00.pyz
Traceback (most recent call last):
  File "c:\users\39558\appdata\local\programs\python\python38\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\39558\appdata\local\programs\python\python38\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\39558\AppData\Local\Programs\Python\Python38\Scripts\pyinstaller.exe\__main__.py", line 9, in 
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\__main__.py", line 111, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\build_main.py", line 844, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\build_main.py", line 791, in build
    exec(code, spec_namespace)
  File "E:\01_政企产品线\04_个人成长\02_python\hello.spec", line 18, in 
    pyz = PYZ(a.pure, a.zipped_data,
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\api.py", line 98, in __init__
    self.__postinit__()
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\datastruct.py", line 158, in __postinit__
    self.assemble()
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\api.py", line 128, in assemble
    self.code_dict = {
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\api.py", line 129, in 
    key: strip_paths_in_code(code)
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\utils.py", line 652, in strip_paths_in_code
    consts = tuple(
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\utils.py", line 653, in 
    strip_paths_in_code(const_co, new_filename)
  File "c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\building\utils.py", line 660, in strip_paths_in_code
    return code_func(co.co_argcount, co.co_kwonlyargcount, co.co_nlocals, co.co_stacksize,
TypeError: an integer is required (got type bytes)
PS E:\01_政企产品线\04_个人成长\02_python> pip install https://github.com/pyinstaller/pyinstaller/archive/develop.tar.gz
Collecting https://github.com/pyinstaller/pyinstaller/archive/develop.tar.gz
  Downloading https://github.com/pyinstaller/pyinstaller/archive/develop.tar.gz
     \ 3.5 MB 293 kB/s
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Requirement already satisfied: setuptools in c:\users\39558\appdata\local\programs\python\python38\lib\site-packages (from PyInstaller==4.0.dev0+a1f92c6a08) (41.2.0)
Requirement already satisfied: altgraph in c:\users\39558\appdata\local\programs\python\python38\lib\site-packages (from PyInstaller==4.0.dev0+a1f92c6a08) (0.16.1)
Requirement already satisfied: pywin32-ctypes>=0.2.0; sys_platform == "win32" in c:\users\39558\appdata\local\programs\python\python38\lib\site-packages (from PyInstaller==4.0.dev0+a1f92c6a08) (0.2.0)
Requirement already satisfied: pefile>=2017.8.1; sys_platform == "win32" in c:\users\39558\appdata\local\programs\python\python38\lib\site-packages (from PyInstaller==4.0.dev0+a1f92c6a08) (2019.4.18)
Requirement already satisfied: future in c:\users\39558\appdata\local\programs\python\python38\lib\site-packages (from pefile>=2017.8.1; sys_platform == "win32"->PyInstaller==4.0.dev0+a1f92c6a08) (0.18.2)
Building wheels for collected packages: PyInstaller
  Building wheel for PyInstaller (PEP 517) ... done
  Created wheel for PyInstaller: filename=PyInstaller-4.0.dev0+a1f92c6a08-py3-none-any.whl size=2917110 sha256=ac73a67ad57f69b7b37ec01dae71147e8c6ef25458b091462090c00e2a6c658c
  Stored in directory: C:\Users\39558\AppData\Local\Temp\pip-ephem-wheel-cache-5qhktnxd\wheels\f7\a9\98\fbdbc2a9056f3770a61982869801ce029ca0a2cefaff4135a9
Successfully built PyInstaller
Installing collected packages: PyInstaller
  Attempting uninstall: PyInstaller
    Found existing installation: PyInstaller 3.5
    Uninstalling PyInstaller-3.5:
      Successfully uninstalled PyInstaller-3.5
Successfully installed PyInstaller-4.0.dev0+a1f92c6a08
PS E:\01_政企产品线\04_个人成长\02_python> pyinstaller -D hello.py                                                      59 INFO: PyInstaller: 4.0.dev0+a1f92c6a08
60 INFO: Python: 3.8.0
61 INFO: Platform: Windows-10-10.0.17763-SP0
62 INFO: wrote E:\01_政企产品线\04_个人成长\02_python\hello.spec
65 INFO: UPX is not available.
66 INFO: Extending PYTHONPATH with paths
['E:\\01_政企产品线\\04_个人成长\\02_python', 'E:\\01_政企产品线\\04_个人成长\\02_python']
66 INFO: checking Analysis
70 INFO: Building because C:\Users\39558\AppData\Local\Programs\Python\Python38\Lib\site-packages\PyInstaller\loader\rthooks\pyi_rth_multiprocessing.py changed
72 INFO: Initializing module dependency graph...
74 INFO: Caching module graph hooks...
80 INFO: Analyzing base_library.zip ...
3145 INFO: Caching module dependency graph...
3243 INFO: running Analysis Analysis-00.toc
3244 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\users\39558\appdata\local\programs\python\python38\python.exe
3304 INFO: Analyzing E:\01_政企产品线\04_个人成长\02_python\hello.py
3305 INFO: Processing module hooks...
3305 INFO: Loading module hook "hook-encodings.py"...
3375 INFO: Loading module hook "hook-xml.py"...
3561 INFO: Looking for ctypes DLLs
3563 INFO: Analyzing run-time hooks ...
3566 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
3572 INFO: Looking for dynamic libraries
3743 INFO: Looking for eggs
3743 INFO: Using Python library c:\users\39558\appdata\local\programs\python\python38\python38.dll
3743 INFO: Found binding redirects:
[]
3746 INFO: Warnings written to E:\01_政企产品线\04_个人成长\02_python\build\hello\warn-hello.txt
3879 INFO: Graph cross-reference written to E:\01_政企产品线\04_个人成长\02_python\build\hello\xref-hello.html
3894 INFO: checking PYZ
3894 INFO: Building PYZ because PYZ-00.toc is non existent
3896 INFO: Building PYZ (ZlibArchive) E:\01_政企产品线\04_个人成长\02_python\build\hello\PYZ-00.pyz
4304 INFO: Building PYZ (ZlibArchive) E:\01_政企产品线\04_个人成长\02_python\build\hello\PYZ-00.pyz completed successfully.
4389 INFO: checking PKG
4389 INFO: Building PKG because PKG-00.toc is non existent
4390 INFO: Building PKG (CArchive) PKG-00.pkg
4403 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
4404 INFO: Bootloader c:\users\39558\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
4404 INFO: checking EXE
4405 INFO: Building EXE because EXE-00.toc is non existent
4405 INFO: Building EXE from EXE-00.toc
4406 INFO: Appending archive to EXE E:\01_政企产品线\04_个人成长\02_python\build\hello\hello.exe
4408 INFO: Building EXE from EXE-00.toc completed successfully.
4410 INFO: checking COLLECT
4410 INFO: Building COLLECT because COLLECT-00.toc is non existent
4412 INFO: Building COLLECT COLLECT-00.toc
4546 INFO: Building COLLECT COLLECT-00.toc completed successfully.

 【Python-GUI开发】六 将py文件转换为exe可执行文件_第2张图片

【Python-GUI开发】六 将py文件转换为exe可执行文件_第3张图片

 

你可能感兴趣的:(python)