Transcrypt 在导入时出现无法导入编解码器错误 json

Transcrypt gives can't import codecs error while importing json

我正在尝试使用命令 transcrypt -b -m -n hello.py 使用 transcrypt 将 python 文件转换为 JS。我在 python 脚本中导入了 JSON:

import json

# the MAIN meaty function!!!
def exec_script_str(script_str, raw_input_lst_json, options_json, finalizer_func):
  if options_json:
    options = json.loads(options_json)
...

我收到此错误消息:

Transcrypt (TM) Python to JavaScript Small Sane Subset Transpiler Version 3.7.16 Copyright (C) Geatec Engineering. License: Apache 2.0

Saving target code in: /home/sarwagya/Desktop/Hello/target/org.transcrypt.runtime.js Saving target code in: /home/sarwagya/Desktop/Hello/target/re.translate.js Saving target code in: /home/sarwagya/Desktop/Hello/target/re.js Saving target code in: /home/sarwagya/Desktop/Hello/target/warnings.js

Error while compiling (offending file last):
File '/home/sarwagya/Desktop/Hello/hello.py', line 2186, at import of:
File '/usr/lib/python3.8/json/init.py', line 108, at import of:
File 'codecs', line 44, namely: Can't import module 'codecs'

Aborted

在此之前我有一些导入似乎有效。由于 Javascript 中有一个 JSON 包,我原以为成绩单会支持这一点。 感谢您的帮助。

完整的标准库尚未移植到 Transcrypt,包括 json 库。在大多数情况下,您可以通过使用 JavaScript 等效库来支持您试图完成的任何事情来解决这个问题,并且可以直接从您的 Python 代码中调用它们。但是 Transcrypt 不会自动为您执行此库映射,因此您必须显式导入 JavaScript 库才能使用它。

自从 Python dictionaries are converted to JavaScript objects 使用 Transcrypt 后,您可以只使用 JSON 数据 as-is。否则,您可以直接调用 built-in JSON.parse()JSON.stringify(),无需导入。