-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't use import in pyscript #696
Comments
The |
Thanks Craig, but I tried this earlier and I get different errors. I also modified my code (function):
The import syntax error did not reoccur in the HA log, so I attempt to run my code in Developer Tools - action. This resulted in a message `Logger: homeassistant.helpers.script.websocket_api_script
`Logger: homeassistant.components.websocket_api.http.connection
My code works sucessfully using IDLE on a Windows 11 system. Am I using the incorrect parts of DATETIME? Regards, Steve |
For a service response, the function needs to return a return {"season": x} |
Thanks again Craig and my apologies, as I know that a function needs to return a dict. I was so wrapped up in trying to solve the import syntax issues that I forgot about the dict. Actually, that piece of code was taken from the main script that I'm using and that does return a dict. I will read the docs again to see if there is anything else that that needs to go before @service. Regards, Steve |
I’m trying to create a simple function using pyscript that needs to import datetime.
I get an error after saving my script in the log:
Exception in </config/pyscript/season.py> line 2: import datetime ^ SyntaxError: invalid syntax (season.py, line 2)
This is my code:
@service(supports_response="only")
import datetime
from datetime import date
def season(return_response=True):
now = date.today()
m = now.month
x = m%12 // 3 + 1
season = x
return season
I have run the same code in an IDLE Shell in Windows (without the @service bit) and it runs without error (and works).
I have installed pyscript as detailed in the documentation, setting the two options for import and global to true in my configuration.yaml file.
My pyscript folder is in the config path (same as configuration.yaml).
Note: I see the deprecated warning as reported in issue #686 in the HASS log.
I have successfully created and run another python script (using pyscript with no imports) earlier, so I know at least it might work. However, it appears that the import statements cause syntax errors, so it looks like “allow_all_imports: true” is not taking effect.
As a double check I completely removed pyscript directories (rm -r) and re-downloaded and installed pyscript again. I made sure that I cancelled the UI configuration so that, I hope, the configuration only comes from the yaml configuration file.
Core 2025.2.0
Supervisor 2025.02.0
Operating System 14.2
Frontend 20250205.0
The text was updated successfully, but these errors were encountered: