Python scripts are running in a sandboxed environment which isvlimited and it may be that the any function is not available there, can’t test right now.
If you want access to python with full functionality and also imports of other librariers, there’s AppDaemon. With PyScript it may be possible as well.