2019年5月2日 星期四

win7+Anaconda+python+pyinstaller 打包執行檔 + cp950 解決方法



        python是一種直譯式的語言,不需要經過編譯就可以執行,不過相對的一定要有對應的 python 直譯器的環境才可以運作,本篇文章將介紹把 python 打包成執行檔的方法,不過打包好的執行檔應該還有平台上的限制,在MS Windows上編譯的程式一般也只能在MS Windows相容的作業系統上運作

Google關鍵字:"anaconda 打包執行檔"

※打包命令(需先安裝 pyinstaller)

pyinstaller -w -F xx\xx\xxx.py

打包的過程會出現各種不同的錯誤訊息,詳細閱讀後找到發生問題的程式區塊及錯誤訊息內容,並 google解決方案,

下面列出幾種作者打包時出現的錯訊息:

1.Cannot find existing PyQt5 plugin directories

解決方案參考連結:
https://www.twblogs.net/a/5c23a691bd9eee16b4a7c6ee
https://github.com/pyinstaller/pyinstaller/issues/3852

這個問題作者單純安裝 PyQt5 就解決了
pip install PyQt5


2.UnicodeDecodeError: 'cp950' codec can't decode ...


解決方案參考連結:
https://stackoverflow.com/questions/49021589/how-do-i-fix-this-cp950-illegal-multibyte-sequence-unicodedecodeerror-when-rea
https://blog.csdn.net/chuatony/article/details/71436582
這是個常見的編碼問題,在 python2 及 python3上的解法略有不同
作者的 python版本為 3.6

找到的發生問題的程式碼後,必然會發現程式在執行
open() 函式後,引用 read() 、write() 之類的函式後發生問題
此時只要在相關函式呼叫時,規定編碼方式為 utf-8 即可
ex:
open(filename, "r")
改成==>
open(filename, "r",encoding="utf-8")