1. 程式人生 > >pyinstaller 打包 python 指令碼成 exe 的坑

pyinstaller 打包 python 指令碼成 exe 的坑

pyinstaller 打包python指令碼成exe,只需要簡單的兩步:

pip install pyinstaller
pyinstaller script.py

或者將動態連結庫也打包到 exe 當中:

pyinstaller --console --onefile script.py

然後在當前目錄下回產生兩個目錄和一個檔案:dist、build、.spec ,exe 就在dist當中。

實際操作中,發生 IndexError: tuple index out of range異常,查詢官網,原來不支援python3.6.

PyInstaller is a program that freezes (packages) Python programs intostand-alone executables, under Windows, Linux, Mac OS X, FreeBSD,Solaris and AIX. Its main advantages over similar tools are thatPyInstaller works withPython 2.7 and 3.3—3.5

, it builds smallerexecutables thanks to transparent compression, it is fullymulti-platform, and use the OS support to load the dynamic libraries,thus ensuring full compatibility.

用 python3.5 重新搗鼓一遍,這次成功了。

可在 exe 執行時,又失敗了:

File "site-packages\urllib3\connectionpool.py", line 28, in <module>
  File "site-packages\urllib3\packages\six.py", line 92, in __get__
  File "site-packages\urllib3\packages\six.py", line 115, in _resolve
  File "site-packages\urllib3\packages\six.py", line 82, in _import_module
ImportError: No module named 'queue'

查了下,有三種方法可以解決:

1、requests 包的版本問題,當前使用的是2.17的版本,換成舊版本問題解決(這個方法確實很扯淡):

pip install requests==2.10

2、更好的辦法,在匯入requests 包的檔案中,顯示的匯入 queue 模組,儘管沒有使用到該模組。

在指令碼中加入:

import queue

同樣問題解決 。

3、在製作的時候,帶上 --hidden-import=queue 引數也可。

D:\ProgramData\python3.5\Scripts\pyinstaller.exe --hidden-import=queue D:\wrokingPro\CSDN_visit.py