UnicodeEncodeError:'mbcs'编解码器无法编码位置0--1中的字符:运行PyInstaller编译的脚本时无效字符

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UnicodeEncodeError:'mbcs'编解码器无法编码位置0--1中的字符:运行PyInstaller编译的脚本时无效字符相关的知识,希望对你有一定的参考价值。

我刚刚完成了一个我正在研究的程序,并且一直想把它编译成单个.exe文件进行分发。我决定使用pyinstaller 3,因为它对我有用,但是在成功编译我的文件后,我在运行程序时收到以下消息。

C:\Users\Luke\Documents\program\dist>viewbot.exe
_ctypes
C:\Users\Luke\AppData\Local\Temp\_MEI59042\_ctypes.pyd
_tkinter
C:\Users\Luke\AppData\Local\Temp\_MEI59042\_tkinter.pyd
Traceback (most recent call last):
  File "<string>", line 7, in <module>
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
  File "C:\Users\Luke\Documents\program\PyInstaller\loader\pyi_importers.py", line 302, in load_module
  File "C:\Python33\lib\tkinter\__init__.py", line 40, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
  File "C:\Users\Luke\Documents\program\PyInstaller\loader\pyi_importers.py", line 474, in load_module
UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character

如果它有帮助,这是我的程序的最小代码示例:

import os
import subprocess
from subprocess import call
import time
import tkinter
from tkinter import filedialog
call("color a", shell=True)
root = tkinter.Tk()
root.withdraw()
print ("Please locate your firefox browser)")
path = filedialog.askopenfilename(parent=root,title="Please locate your firefox browser")
path = path + " 0"
FNULL = open(os.devnull, 'w')
viewed = 0
url = "http://google.com"
refresh = 15
views = 5
call("cls", shell=True)
for i in range(views):
    proc = subprocess.Popen(path.format(url))
    time.sleep(refresh)
    viewed = viewed + 1
    print ("Viewed", viewed, "time")
    proc.terminate()
    call("cls", shell=True)
print ("Viewing finished in", refresh * views, "seconds")
time.sleep(5)

不太清楚在这里做什么所以任何帮助将不胜感激:)

答案

我有相同的错误消息。我通过在包含要打开的文件的路径的字符串中替换\ by \\来修复它。

以上是关于UnicodeEncodeError:'mbcs'编解码器无法编码位置0--1中的字符:运行PyInstaller编译的脚本时无效字符的主要内容,如果未能解决你的问题,请参考以下文章

UnicodeEncodeError: 'ascii' codec can't encode character u'u5929' in position 2(

UnicodeEncodeError: 'ascii' codec can't encode characters

UnicodeEncodeError: 'ascii' codec can't encode characters in

UnicodeEncodeError: 'gbk' codec can't encode character 'xbb' in position

UnicodeEncodeError: 'ascii' codec can't encode character...的解决方法

UnicodeEncodeError: 'gbk' codec can't encode character 'u25aa' in position 15: (