Python_script not work

Goodmorning, I’m trying to run a python script inside automations.yaml but it does not work and I would ask help. I write down the lines I wrote until now.

configurations.yaml:

switch:
  - platform: rpi_gpio
    ports:
      13: luci
    invert_logic: true

shell_command:
  hello_world: 'python3 /config/python_scripts/hello_world.py'
python_script:
  hello_world: /config/python_scripts/hello_world.py

automations.yaml:

- alias: Luci
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: switch.luci
    to: 'on'
    for:
      milliseconds: 500
  action:
    - service: switch.turn_off
      entity_id: switch.luci
    - service: notify.notify
      data:
        message: "luci"
- alias: test
  trigger:
    - minutes: "/2"
      platform: time_pattern 
  action:
    - service: python_script.hello_world
      data:
        entity_id: switch.luci 
    - service: notify.notify
      data_template:
        message: " SpeedTest, download speed = {{ states('sensor.speedtest_download') }} MB/s. "
        message: "invio"

instead of

    - service: python_script.hello_world
      data:
        entity_id: switch.luci 

i tryed with the following but it not work too:

- service: shell_command.hello_world

/config/python_scripts/hello_world.py:

import datetime
entity_id = data.get("entity_id")
if entity_id is not None:
 service_data = {"entity_id": entity_id}
 hass.services.call("switch", "turn_on", service_data, False)

f = open("/config/python_scripts/test.txt","a")
f.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
f.write("\n")
f.close()

Can someone help me to understand why it not work?
thank you

Python scrips are running in a sandbox environment. You can’t import other libraries as far as I know. So I assume your problem is the import datetime.

Did you get any errors in the logs?

yes you are right. There is the following line in the log:

rror executing script: __import__ not found
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 205, in execute
    exec(compiled.code, restricted_globals)
  File "hello_world.py", line 1, in <module>
ImportError: __import__ not found

there is no alternative to enable import libraries? this is jsut my initial python code to start more complex codes. For these I need import many libraries.
thank you very much for your availability.

You can use AppDaemon, there you can use as many imports as you want and also install additional libraries through pip.

Could you not just send the timestamp from homeassistant as a variable?

    - service: python_script.hello_world
      data:
        entity_id: switch.luci
        time: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"