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

标签 javascript json python-3.x transcrypt

我正在尝试使用命令 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 数据。否则你可以调用内置的JSON.parse()JSON.stringify()直接不需要进口。

关于javascript - Transcrypt 在导入 json 时出现无法导入编解码器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63339044/

相关文章:

javascript - ECMAScript 2015 临时死区

python - python中的setuptools错误

python - python 中的 turtle 迷宫。我不知道如何避免 turtle 穿墙和作弊

python - 如何检查迭代器是否实际上是迭代器容器?

javascript - 提取一个对象数组内的属性,该对象数组位于另一个对象数组内,并将结果作为字符串数组

javascript - 使用 JQuery 删除文本并更改嵌入在 div 中的 div 中的 bgimage

javascript - 如何将对象添加到数组列表

Ruby json解析错误: unexpected token

java - 子类的 Jersey 和 Jackson 序列化不包括额外的属性

javascript - 如何遍历嵌套对象的所有键和值?