(1)安裝:
用傳統的pip install pyinstaller出錯,在https://pypi.org/project/PyInstaller/#files上下載PyInstaller-3.4.tar.gz (3.5 MB),解壓,cmd設置當前路徑未,解壓到的文件夾位置,dos上輸入 python setup.py install。
當-d all時候,打開生成的可執行文件,會輸出各種信息,比如調用的包有哪些,分別來自哪里(imports 選項)
- all: All three of the following options.
- imports: specify the -v option to the underlying Python interpreter, causing it to print a message each time a module is initialized, showing the place (filename or built-in module) from which it is loaded. See https://docs.python.org/3/using/cmdline.html#id4.
- bootloader: tell the bootloader to issue progress messages while initializing and starting the bundled app. Used to diagnose problems with missing imports.
- noarchive: instead of storing all frozen Python source files as an archive inside the resulting executable, store them as files in the resulting output directory
(2)使用注意事項:
(a)如果在源碼中用相對路徑調用圖片文件或者圖標文件,打包時會出現如下圖的錯誤:
解決方法很簡單,只需要在源碼中,相對路徑改為絕對路徑,然后在打包時,再將絕對路徑傳入pyinstaller 命令即可。
舉例說明,具體為:
(b)使用圖片:
在源碼中,使用絕對路徑:
img1=PhotoImage(file=r'F:\FIELS\tkinGui\Example_pyinstaller\1.gif') Button(win,image=img1).pack()
使用pyinstaller時,首先用cd將dos現在路徑改為目標打包py文件的位置,然后 pyinstaller -w --add-data="F:\FIELS\tkinGui\Example_pyinstaller\1.gif;image" imgButton.py
即可。注意,這里--add-data="源地址;目標地址“,源地址為搜索文件的地址,這里使用絕對路徑,";"為os.pathsep(平台路徑分割符,window為;其他為:),目標地址必須填寫image,是代碼運行時候所在文件夾下的文件名“image”,詳見https://pyinstaller.readthedocs.io/en/stable/usage.html。
(c)使用圖標:
在源碼中,使用絕對路徑:
root=Tk() root.title('BRN1.0') root.resizable(0,0) root.iconbitmap(r'F:\FIELS\tkinGui\BatchReName\BatchReName.ico')
pyinstaller 命令: pyinstaller -w -i="F:\FIELS\tkinGui\BatchReName\BatchReName.ico" BatchReName.py
(4)打包后,代碼中的ico,及image搜索路徑將依賴於.py文件中設定的路徑,為了保證打包形成exe文件后,仍能找到用戶電腦中的路徑,.py文件中的路徑最好設置成相對路徑,再結合os.path.abspath形成用戶電腦上的絕對路徑。如:
ImagePath=r'.\image\BatchReName.ico' def resource_path(relative_path): """ return absolute path """ if hasattr(sys,'_MEIPASS'): base_path=sys._MEIPASS else: base_path=os.path.abspath('.') return os.path.join(base_path,relative_path) ImagePath=resource_path(ImagePath)
將imagepath從相對路徑轉化為用戶電腦上的絕對路徑。
(5)打包腳本后運行,提示vcruntime140.dll 未在指定的window上運行,
找了下,說是版本不同,或者upx壓縮錯誤,然后就加了下 --noupx命令,打包完,軟件大了一圈,不過再運行就沒報錯了。UPX一個免費的對各個操作系統可執行文件進行壓縮的可執行打包器(https://upx.github.io/),PyInstaller looks for UPX on the execution path or the path specified with the --upx-dir
option. If UPX exists, PyInstaller applies it to the final executable, unless the --noupx
option was given. UPX has been used with PyInstaller output often, usually with no problems.
具體原因應該是安裝的ups是64位的,然后壓縮了32位的,導致報錯了
參考文章鏈接:https://blog.csdn.net/jpch89/article/details/81183019
(6)打包時會出現問題Cannot find existing PyQt5 plugin directories,解決方法就是用everything搜索PyQt5,找到 /Library/plugins路徑下的PyQt5文件夾,將里面的dll動態庫pyqt5qmlplugin.dll復制出來
按照錯誤提示的路徑,一個個的新建文件夾,形成目錄C:\qt5b\qt_1524647842210_h_env\Library\plugins,將剛才復制出來的dll動態庫拷貝進去即可。親測有效。
————————————————
原文鏈接:https://blog.csdn.net/lixiaoyu101/article/details/84620336
(7)PyQt5打包后,自己電腦上運行可以,但拷貝到相同系統的其他電腦上,not find Qt platform plugin “windows”,全網找了半天,最后通過,將platform文件夾下的所有dll文件拷貝到跟exe文件一個目錄下,成功運行。感謝博文https://blog.csdn.net/okfu_DL/article/details/84566545給我的靈感,不過跟它的方法不太一樣,它是將整個platform文件夾拷貝到跟exe一個目錄的,我試了下沒成功。platform文件在python文件下,如E:\software\py\Lib\site-packages\PyQt5\Qt\plugins\platforms,注意這個platform應該是負責打包的解釋器下的platform文件。
當本機解釋器拋出not find platform警示框后,在環境變量中添加QT_QPA_PLATFORM_PLUGIN_PATH變量,值為,比如我的是C:\Users\AppData\Local\Programs\Python\Python36\Lib\site-packages\PyQt5\Qt\plugins
(8)打包成功后,運行.exe報錯: ModuleNotFoundError: No module named ’numpy.core.__dtype_ctypes’:
總結一下: 這些問題主要從GitHub上該項目的issue模塊得到了解決方案(我們不是孤獨的,大家都遇到了同樣的問題)
可以通過兩種方式得到解決:
將下述代碼放到hook-numpy.core.py文件里(當然你需要把這個路徑在命令行或者.spec中告訴pyinstaller)。

import sys sys.setrecursionlimit(5000)
然后,pyinstaller xxx.spec就可以了