程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Unicode decodeerror occurs when Python is packaged into exe

編輯:Python

python Pack it up exe Post run appears UnicodeDecodeError The problem of


Here's the thing , I use python I wrote a script , This script needs to pass in parameters from the console , And may contain Chinese . After writing the script, run it completely ok:

So I use pyinstaller Packed into exe file , There is no problem with the console .

But in use c# perhaps python Execute this exe But there was UnicodeDecodeError The problem of :

import subprocess
p = subprocess.Popen('my_trans.exe zh jp Zhang San baidu', shell=False, stdout=subprocess.PIPE)
out, err = p.communicate()
for line in out.splitlines():
print(line.decode("UTF-8"))


At first I thought it was the problem of parameter coding , So I tried many schemes , such as : Pass in parameters after encoding , Decoding in the program .
unfortunately , It won't work , I don't know why , In principle, there should be no such problem after encoding the parameters .

After trying unsuccessfully , A meal of Baidu , Almost all plus #-*- coding: utf-8 -*- In this way , I tried it, too , I don't have much hope , Because this is just the code of the declaration file , To prevent Chinese from being garbled , As I expected , Still not .

A large number of people on the Internet say that they can use the following methods to temporarily change the system code :

 import sys
reload(sys)
sys.setdefaultencoding("utf-8")

This way should feel feasible , But anyway sys No, setDefaultencoding How dare you believe this method ? I search for python3 Change the default encoding problem , The result of the search is still this way . Still keep checking , debugging , check , debugging , Finally, it was found that the program was print The Chinese parameters all appear UnicodeDecodeError.

So after continuing to check in this direction , Finally, I found a reliable blog , Here's an apology , Because I just wanted to go back and find this blog, but I couldn't find it , So I can't share his blog address with you .
The code to solve the output garbled code is this :

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")

This code can solve the problem of output parameters UncodeDecodeError The problem of , So my problem is actually the problem of input parameters , So I tried to add the following code :

sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding="utf-8")

then , The problem is solved with ease .

Now let's analyze it. In fact, I think the principle should be like this , First, the coding of input parameters should be determined by the console , So whether you use the console to execute exe still py Files may all be encoded using the system UTF8, But the program may use when receiving parameters Unicode Code to accept , The problem of code conversion is involved, so UnicodeDecodeError. These two lines of code specify that the input and output codes are UTF8, Therefore, after unified coding, this problem naturally does not exist .

The above is what we are looking for Bug A note of , It took two days . People who want to see don't get lost , Welcome to leave a message to discuss !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved