I am trying to convert a python file to JS using transcrypt using the command transcrypt -b -m -n hello.py
. I have an import to JSON in my python script:
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)
...
I get this error message:
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
I have a number of imports before this which seem to work. Since there is a JSON package in Javascript I would have thought transcript would support this. Thanks for the help.
The full standard library has not been ported to Transcrypt yet, the json library included. In most cases you can get around this by using a JavaScript equivalent library to support whatever you are trying to accomplish, and they can be called directly from your Python code. But Transcrypt doesn't automatically do this library mapping for you, so you'll have to explicitly import the JavaScript library to use it.
Since Python dictionaries are converted to JavaScript objects with Transcrypt, you may be able to just use the JSON data as-is. Otherwise you can call the built-in JSON.parse()
and JSON.stringify()
directly with no imports necessary.