Replies: 2 comments
-
|
Hii TrevskiUK, The root cause is that PyScript deliberately doesn't support built in functions like open, read, and write to avoid blocking the main event loop and for security reasons so open() simply isn't available in pyscript context, which is why parsed_data stays None and you get the TypeError. You could use io.open via task.executor import io, json, os
@pyscript_executor
def write_file(path, content):
os.makedirs(os.path.dirname(path), exist_ok=True)
with io.open(path, "w") as f:
f.write(content)
@pyscript_executor
def read_json(path):
with io.open(path, "r", encoding="utf-8") as f:
return json.load(f)
def userproc(action=None, id=None):
write_file("/homeassistant/myah/outbox/userproc_out.myah", "made it this far: 01")
parsed_data = read_json("/homeassistant/.storage/auth")
user_ids = [user["id"] for user in parsed_data["data"]["users"]]
log.info(f"user_ids: {user_ids}")Or put file I/O in a separate native python module in a directory outside config/pyscript/, then import and call it via task.executor. The @pyscript_executor decorator handles wrapping the function in task.executor automatically. Note that allow_all_imports: true must be set in your pyscript config. |
Beta Was this translation helpful? Give feedback.
-
|
Thank you Dave! Much appreciated.
…On Fri, 19 Jun 2026 at 22:19, Dave Perera ***@***.***> wrote:
Hii TrevskiUK,
The root cause is that PyScript deliberately doesn't support built in
functions like open, read, and write to avoid blocking the main event loop
and for security reasons so open() simply isn't available in pyscript
context, which is why parsed_data stays None and you get the TypeError.
You could use io.open via task.executor
import io, json, os
@pyscript_executordef write_file(path, content):
os.makedirs(os.path.dirname(path), exist_ok=True)
with io.open(path, "w") as f:
f.write(content)
@pyscript_executordef read_json(path):
with io.open(path, "r", encoding="utf-8") as f:
return json.load(f)
def userproc(action=None, id=None):
write_file("/homeassistant/myah/outbox/userproc_out.myah", "made it this far: 01")
parsed_data = read_json("/homeassistant/.storage/auth")
user_ids = [user["id"] for user in parsed_data["data"]["users"]]
log.info(f"user_ids: {user_ids}")
Or put file I/O in a separate native python module in a directory outside
config/pyscript/, then import and call it via task.executor.
The @pyscript_executor decorator handles wrapping the function in
task.executor automatically. Note that allow_all_imports: true must be set
in your pyscript config.
—
Reply to this email directly, view it on GitHub
<#857?email_source=notifications&email_token=AJIOCOFEYCHFY34XDZANDLD5AWU47A5CNFSNUABIM5UWIORPF5TWS5BNNB2WEL2ENFZWG5LTONUW63SDN5WW2ZLOOQXTCNZTGY4TCOJWUZZGKYLTN5XKMYLVORUG64VFMV3GK3TUVRTG633UMVZF6Y3MNFRWW#discussioncomment-17369196>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJIOCOBTYZCKPYRM56IEHPD5AWU47AVCNFSNUABIKJSXA33TNF2G64TZHMZDQMZYGQ3TSNJXHNCGS43DOVZXG2LPNY5TCMBSHA4DKNJUUF3AE>
.
Triage notifications, keep track of coding agent tasks and review pull
requests on the go with GitHub Mobile for iOS
<https://github.com/notifications/mobile/ios/AJIOCOGY3DVA7TL6J5IRHID5AWU47A5CNFSNUABIM5UWIORPF5TWS5BNNB2WEL2ENFZWG5LTONUW63SDN5WW2ZLOOQXTCNZTGY4TCOJWUZZGKYLTN5XKMYLVORUG64VFMV3GK3TUVJTG633UMVZF62LPOM>
and Android
<https://github.com/notifications/mobile/android/AJIOCOCI3BCEWVDCB622HMT5AWU47A5CNFSNUABIM5UWIORPF5TWS5BNNB2WEL2ENFZWG5LTONUW63SDN5WW2ZLOOQXTCNZTGY4TCOJWUZZGKYLTN5XKMYLVORUG64VFMV3GK3TUVZTG633UMVZF6YLOMRZG62LE>.
Download it today!
You are receiving this because you authored the thread.Message ID:
***@***.***
com>
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
New to this add-on, and struggling.
Here is my code:
When run from HA Developer Actions,:
No text file is written "/homeassistant/myah/outbox/userproc_out.myah"
Something is not working with the JSON parsing of the file "/homeassistant/.storage/auth", as I get the following in the log:
This is all very frustrating because my original standalone version of the script works perfectly when executed from the HA terminal.
Aaargh.
Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions