Skip to content
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

Python Scripts : strptime no longer works #132469

Closed
MisterRadish opened this issue Dec 6, 2024 · 15 comments · Fixed by #133159
Closed

Python Scripts : strptime no longer works #132469

MisterRadish opened this issue Dec 6, 2024 · 15 comments · Fixed by #133159

Comments

@MisterRadish
Copy link

MisterRadish commented Dec 6, 2024

The problem

Converting string to datetime using strptime now produces error Error executing script (KeyError): '__import__' when running the script.)
example:
bookingdate = datetime.datetime.strptime("2024-04-01", "%Y-%m-%d")

What version of Home Assistant Core has the issue?

2024.12.0
2024.12.1
2024.12.2

What was the last working version of Home Assistant Core?

2024.11.x

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Python Scripts

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

@MisterRadish MisterRadish changed the title Python Script : strptime no longer works Python Scripts : strptime no longer works Dec 6, 2024
@gjohansson-ST
Copy link
Member

I think you might need to import datetime first instead of using directly.

@home-assistant
Copy link

home-assistant bot commented Dec 6, 2024

@MisterRadish
Copy link
Author

MisterRadish commented Dec 6, 2024

I think you might need to import datetime first instead of using directly.

Thank you for the response.

In 2024.11.x
datetime.datetime.strptime("2024-04-01", "%Y-%m-%d") worked correctly without import

in 2024.12.0 if I try
datetime.datetime.strptime("2024-04-01", "%Y-%m-%d") causes " import'" error
Just adding from datetime import datetime causes "import not found" error
Just adding import datetime causes "import not found" error

What has changed and what am I missing?

@MisterRadish
Copy link
Author

I think you might need to import datetime first instead of using directly.

Also this issue with integration:Python Scripts not pyscript?

@home-assistant
Copy link

home-assistant bot commented Dec 6, 2024

@gjohansson-ST
Copy link
Member

Nothing changed in the source code and it should work like you had it before so no idea right now actually.
I would need to test myself to see where it goes wrong.

@MisterRadish
Copy link
Author

Nothing changed in the source code and it should work like you had it before so no idea right now actually. I would need to test myself to see where it goes wrong.

Further thoughts:-
today = datetime.datetime.now() works ok so the datetime module is accessiable but not the strptime function.

@MisterRadish
Copy link
Author

MisterRadish commented Dec 6, 2024

On the off chance anybody else might be experiencing an issue with strptime I have found that:-
dt_util.parse_datetime("2024-05-01") and
datetime.datetime.fromisoformat("2024-05-01") both seem to work as long as your date is in the YY-mm-dd format. Not all my dates are however.

@straub
Copy link

straub commented Dec 10, 2024

In case it helps anyone: I ran into a very similar issue with datetime.datetime.strptime('14:00:00', '%H:%M:%S').time() in my python script under 2024.12. Since I was using it only to get a time object, I was able to replace it with calling datetime.time(14, 0, 0) directly.

This issue helped me identify the problematic call and try alternatives, so thank you! (The specific error I was experiencing was Error executing script (KeyError): '__import__' when running the script.)

@carlos-leopoly
Copy link

I have also run to the same issue. I’m using python_script heavily and several logics rely on strptime function. So I’m looking forward to the resolution.
I don’t have the competance and time to fix but I would be happy to help by testing or any other way if needed.

@WhoTheHeck
Copy link

I have the same issue since 2024.12.

can not use datetime.datetime.strptime anymore. Thanks to @MisterRadish, I got a workaround working based on datetime.datetime.fromisoformat

@gjohansson-ST
Copy link
Member

So I gave this a test now on both dev and 2024.12.0 and I seem to have no issue at all.
Test script:

bookingdate = datetime.datetime.strptime("2024-04-01", "%Y-%m-%d")
logger.info(f"Date {bookingdate}")

Output:

2024-12-11 18:50:46.671 INFO (SyncWorker_5) [homeassistant.components.python_script] Executing testing.py: {}
2024-12-11 18:50:46.672 INFO (SyncWorker_5) [homeassistant.components.python_script.testing.py] Date 2024-04-01 00:00:00
2024-12-11 18:50:46.672 DEBUG (SyncWorker_5) [homeassistant.components.python_script] Output of python_script: `testing.py`:
{}

On dev we are still on py3.12 so I wonder if there is something that has changed here between the python versions that might have an impact.

@MisterRadish
Copy link
Author

MisterRadish commented Dec 11, 2024

On dev we are still on py3.12 so I wonder if there is something that has changed here between the python versions that might have an impact.

Yup - the move to python 3.13 in 2024.12.0 could be the cause

@gjohansson-ST
Copy link
Member

I can at least at this stage confirm that py3.13 is the cause of the issue

@MisterRadish
Copy link
Author

A fix has been released in 2024.12.4. Thank you for your help @gjohansson-ST

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants