My PYTHONPATH system variable is set to the following and python.exe and pythonw.exe are both in the D:\\apps\\Python\\ArcGIS10.5 folder. This script works fine in the Python window in ArcMAP.
D:\\apps\\Python\\ArcGIS10.5; D:\\apps\\ArcGIS\\Desktop10.5\\bin