我正在尝试使用命令 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/