Python使用 execjs 出现 gbk报错的问题记录和解决

928 阅读1分钟

问题产生:

在使用execjs调用js的时候产生的问题。
一个很奇怪的问题,家里和办公的电脑同一个包,办公电脑却报错。

下面展示报错代码片。

Exception in thread Thread-1:
Traceback (most recent call last):
  File "E:\codes\python\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "E:\codes\python\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "E:\codes\python\lib\subprocess.py", line 1238, in _readerthread
    buffer.append(fh.read())
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 41: illegal multibyte sequence

问题解决:

按提示进入 xxx\python\lib\subprocess.py( 双击报错提示,可快捷进入) 在 class Popen(object): 下的, 将参数 encoding="None" 修改为 encoding="utf-8" 即可。

def __init__(self, args, bufsize=-1, executable=None,
             stdin=None, stdout=None, stderr=None,
             preexec_fn=None, close_fds=True,
             shell=False, cwd=None, env=None, universal_newlines=None,
             startupinfo=None, creationflags=0,
             restore_signals=True, start_new_session=False,
             pass_fds=(), *, encoding="utf-8", errors=None, text=None):
    """Create new Popen instance."""
    _cleanup()