1. 程式人生 > >完美解決python3.6環境下,使用pyinstaller打包.exe時報錯的情況。

完美解決python3.6環境下,使用pyinstaller打包.exe時報錯的情況。

初次使用python就被python強大的第三方庫所吸引。也正因很多都是第三方庫,存在更新不及時的情況也很多。
下載pyinstaller

為了偷懶我是直接使用:pip install pyinstaller 

然後開始打包:pyinstaller.exe -F -w D:\python\1\HelloWorld.py

結果報錯:

D:\python\pyinstaller>pyinstaller.exe -F -w D:\python\1\HelloWorld.py
221 INFO: PyInstaller: 3.2.1
221 INFO: Python: 3.6.0
221 INFO: Platform: Windows-10-10.0.14393-SP0
221 INFO: wrote D:\python\pyinstaller\HelloWorld.spec
275 INFO: UPX is available.
275 INFO: Extending PYTHONPATH with paths
['D:\\python\\1', 'D:\\python\\pyinstaller']
290 INFO: checking Analysis
292 INFO: Building Analysis because out00-Analysis.toc is non existent
293 INFO: Initializing module dependency graph...
296 INFO: Initializing module graph hooks...
298 INFO: Analyzing base_library.zip ...
Traceback (most recent call last):
  File "C:\Programs\Python\Python36\Scripts\pyinstaller-script.py", line 11, in <module>
    load_entry_point('PyInstaller==3.2.1', 'console_scripts', 'pyinstaller')()
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\__main__.py", line 90, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\__main__.py", line 46, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\building\build_main.py", line 788, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\building\build_main.py", line 734, in build
    exec(text, spec_namespace)
  File "<string>", line 16, in <module>
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\building\build_main.py", line 212, in __init__
    self.__postinit__()
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\building\datastruct.py", line 161, in __postinit__
    self.assemble()
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\building\build_main.py", line 317, in assemble
    excludes=self.excludes, user_hook_dirs=self.hookspath)
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\depend\analysis.py", line 560, in initialize_modgraph
    graph.import_hook(m)
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1509, in import_hook
    source_package, target_module_partname, level)
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1661, in _find_head_package
    target_module_headname, target_package_name, source_package)
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\depend\analysis.py", line 209, in _safe_import_module
    module_basename, module_name, parent_package)
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2077, in _safe_import_module
    module_name, file_handle, pathname, metadata)
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2167, in _load_module
    self._scan_code(m, co, co_ast)
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2585, in _scan_code
    module, module_code_object, is_scanning_imports=False)
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2831, in _scan_bytecode
    global_attr_name = get_operation_arg_name()
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2731, in get_operation_arg_name
    return module_code_object.co_names[co_names_index]
IndexError: tuple index out of range

網上查閱無數資料後,發現原來pyinstaller不支援python3.6,官方給出的解決方案是用開發版替換(下載地址:https://github.com/pyinstaller/pyinstaller)

然後根據網友的指引,下載後直接用開發版的pyinstaller資料夾替換掉原來安裝的。

開始再次執行:pyinstaller.exe -F -w D:\python\1\HelloWorld.py

結果還是報錯:

D:\python\pyinstaller>pyinstaller.exe -F -w D:\python\1\HelloWorld.py
98 WARNING: Internal error: early pywin32 import was introduced
Traceback (most recent call last):
  File "C:\Programs\Python\Python36\Scripts\pyinstaller-script.py", line 11, in <module>
    load_entry_point('PyInstaller==3.2.1', 'console_scripts', 'pyinstaller')()
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\__main__.py", line 62, in run
    import PyInstaller.building.build_main
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\building\build_main.py", line 32, in <module>
    from ..depend import bindepend
  File "c:\programs\python\python36\lib\site-packages\PyInstaller\depend\bindepend.py", line 41, in <module>
    import pefile
ModuleNotFoundError: No module named 'pefile'

查了很多資料,發現很多人都只是隻言片語,不容易解決問題,最終功夫不負有心人

在開發版目錄下執行:python setup.py install 重新安裝,問題迎刃而解!

D:\python\pyinstaller>pyinstaller.exe -F -w D:\python\1\HelloWorld.py
0 WARNING: Internal error: early pywin32 import was introduced
147 INFO: PyInstaller: 3.3.dev0+007f78f65
147 INFO: Python: 3.6.0
147 INFO: Platform: Windows-10-10.0.14393-SP0
147 INFO: wrote D:\python\pyinstaller\HelloWorld.spec
200 INFO: UPX is available.
200 INFO: Extending PYTHONPATH with paths
['D:\\python\\1', 'D:\\python\\pyinstaller']
200 INFO: checking Analysis
200 INFO: Building Analysis because out00-Analysis.toc is non existent
200 INFO: Initializing module dependency graph...
216 INFO: Initializing module graph hooks...
216 INFO: Analyzing base_library.zip ...
4949 INFO: running Analysis out00-Analysis.toc
4986 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Programs\Python\Python36\python.exe
5065 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of C:\Programs\Python\Python36\python.exe
5149 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of C:\Programs\Python\Python36\python.exe
5218 WARNING: lib not found: api-ms-win-crt-locale-l1-1-0.dll dependency of C:\Programs\Python\Python36\python.exe
5303 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of C:\Programs\Python\Python36\python.exe
5387 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of C:\Programs\Python\Python36\python.exe
5470 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of C:\Programs\Python\Python36\VCRUNTIME140.dll
5550 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of C:\Programs\Python\Python36\VCRUNTIME140.dll
5618 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of C:\Programs\Python\Python36\VCRUNTIME140.dll
5687 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of C:\Programs\Python\Python36\VCRUNTIME140.dll
5779 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of C:\Programs\Python\Python36\VCRUNTIME140.dll
5950 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of C:\Programs\Python\Python36\python36.dll
6035 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of C:\Programs\Python\Python36\python36.dll
6103 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of C:\Programs\Python\Python36\python36.dll
6187 WARNING: lib not found: api-ms-win-crt-filesystem-l1-1-0.dll dependency of C:\Programs\Python\Python36\python36.dll
6266 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of C:\Programs\Python\Python36\python36.dll
6350 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of C:\Programs\Python\Python36\python36.dll
6436 WARNING: lib not found: api-ms-win-crt-locale-l1-1-0.dll dependency of C:\Programs\Python\Python36\python36.dll
6504 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of C:\Programs\Python\Python36\python36.dll
6595 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of C:\Programs\Python\Python36\python36.dll
6667 WARNING: lib not found: api-ms-win-crt-process-l1-1-0.dll dependency of C:\Programs\Python\Python36\python36.dll
6736 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of C:\Programs\Python\Python36\python36.dll
6830 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of C:\Programs\Python\Python36\python36.dll
6836 INFO: Caching module hooks...
6841 INFO: Analyzing D:\python\1\HelloWorld.py
6845 INFO: Loading module hooks...
6845 INFO: Loading module hook "hook-encodings.py"...
6921 INFO: Loading module hook "hook-pydoc.py"...
6936 INFO: Loading module hook "hook-xml.py"...
7268 INFO: Looking for ctypes DLLs
7268 INFO: Analyzing run-time hooks ...
7288 INFO: Looking for dynamic libraries
7383 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_ssl.pyd
7458 WARNING: lib not found: api-ms-win-crt-utility-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_ssl.pyd
7536 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_ssl.pyd
7622 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_ssl.pyd
7690 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_ssl.pyd
7768 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_ssl.pyd
7837 WARNING: lib not found: api-ms-win-crt-filesystem-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_ssl.pyd
7923 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_ssl.pyd
8006 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_ssl.pyd
8088 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_ssl.pyd
8188 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\unicodedata.pyd
8253 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\unicodedata.pyd
8338 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\unicodedata.pyd
8430 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\pyexpat.pyd
8491 WARNING: lib not found: api-ms-win-crt-utility-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\pyexpat.pyd
8591 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\pyexpat.pyd
8658 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\pyexpat.pyd
8738 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\pyexpat.pyd
8823 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_hashlib.pyd
8907 WARNING: lib not found: api-ms-win-crt-utility-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_hashlib.pyd
8992 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_hashlib.pyd
9054 WARNING: lib not found: api-ms-win-crt-time-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_hashlib.pyd
9138 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_hashlib.pyd
9207 WARNING: lib not found: api-ms-win-crt-convert-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_hashlib.pyd
9292 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_hashlib.pyd
9370 WARNING: lib not found: api-ms-win-crt-environment-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_hashlib.pyd
9444 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_hashlib.pyd
9540 WARNING: lib not found: api-ms-win-crt-stdio-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_bz2.pyd
9607 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_bz2.pyd
9708 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_bz2.pyd
9786 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_bz2.pyd
9854 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_bz2.pyd
9938 WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_lzma.pyd
10021 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_lzma.pyd
10092 WARNING: lib not found: api-ms-win-crt-string-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_socket.pyd
10170 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\_socket.pyd
10254 WARNING: lib not found: api-ms-win-crt-runtime-l1-1-0.dll dependency of C:\Programs\Python\Python36\DLLs\select.pyd
10270 INFO: Looking for eggs
10270 INFO: Using Python library C:\Programs\Python\Python36\python36.dll
10270 INFO: Found binding redirects:
[]
10270 INFO: Warnings written to D:\python\pyinstaller\build\HelloWorld\warnHelloWorld.txt
10308 INFO: checking PYZ
10308 INFO: Building PYZ because out00-PYZ.toc is non existent
10323 INFO: Building PYZ (ZlibArchive) D:\python\pyinstaller\build\HelloWorld\out00-PYZ.pyz
10960 INFO: Building PYZ (ZlibArchive) D:\python\pyinstaller\build\HelloWorld\out00-PYZ.pyz completed successfully.
10972 INFO: checking PKG
10972 INFO: Building PKG because out00-PKG.toc is non existent
10973 INFO: Building PKG (CArchive) out00-PKG.pkg
11024 INFO: Executing - upx --lzma -q C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\vcruntime140.dll
11286 INFO: Updating manifest in C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\python36.dll
11288 INFO: Updating resource type 24 name 2 language 1033
11308 INFO: Executing - upx --lzma -q C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\python36.dll
13635 INFO: Updating manifest in C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\_ssl.pyd
13635 INFO: Updating resource type 24 name 2 language 1033
13651 INFO: Executing - upx --lzma -q C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\_ssl.pyd
14882 INFO: Updating manifest in C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\unicodedata.pyd
14882 INFO: Updating resource type 24 name 2 language 1033
14897 INFO: Executing - upx --lzma -q C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\unicodedata.pyd
15566 INFO: Updating manifest in C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\pyexpat.pyd
15566 INFO: Updating resource type 24 name 2 language 1033
15581 INFO: Executing - upx --lzma -q C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\pyexpat.pyd
15868 INFO: Updating manifest in C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\_hashlib.pyd
15869 INFO: Updating resource type 24 name 2 language 1033
15880 INFO: Executing - upx --lzma -q C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\_hashlib.pyd
16994 INFO: Updating manifest in C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\_bz2.pyd
16994 INFO: Updating resource type 24 name 2 language 1033
17009 INFO: Executing - upx --lzma -q C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\_bz2.pyd
17184 INFO: Updating manifest in C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\_lzma.pyd
17184 INFO: Updating resource type 24 name 2 language 1033
17199 INFO: Executing - upx --lzma -q C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\_lzma.pyd
17496 INFO: Updating manifest in C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\_socket.pyd
17496 INFO: Updating resource type 24 name 2 language 1033
17512 INFO: Executing - upx --lzma -q C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\_socket.pyd
17669 INFO: Updating manifest in C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\select.pyd
17669 INFO: Updating resource type 24 name 2 language 1033
17685 INFO: Executing - upx --lzma -q C:\Users\andrew\AppData\Roaming\pyinstaller\bincache01_py36_64bit\select.pyd
18037 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
18053 INFO: Bootloader c:\programs\python\python36\lib\site-packages\pyinstaller-3.3.dev0+007f78f65-py3.6.egg\PyInstaller\bootloader\Windows-64bit\runw.exe
18053 INFO: checking EXE
18053 INFO: Building EXE because out00-EXE.toc is non existent
18053 INFO: Building EXE from out00-EXE.toc
18053 INFO: Appending archive to EXE D:\python\pyinstaller\dist\HelloWorld.exe
18084 INFO: Building EXE from out00-EXE.toc completed successfully.