The accepted answer on StackExchange works for ArcGIS Desktop, which is what the OP was asking about, but the accepted answer won't work for ArcGIS Pro.
The arcpy.mapping Layer function was removed in ArcGIS Pro, it has been replaced with LayerFile. If one looks at the Summary for Layer:
Summary Provides access to layer properties and methods. It can either reference layers in a map document (.mxd) or layers in a layer (.lyr) file. |
It turns out, the accepted answer on StackExchange was never supposed to work but it does. Originally, arcpy.mapping Layer was designed to work on layers in MXDs or existing layer files, but there was a bug that allowed it to work directly against some data sources (but not all). Since the hack was fairly common place, the bug wasn't fixed, until ArcGIS Pro. To avoid confusion over changed functionality and having people submit bug reports, Layer was renamed LayerFile to emphasize how it is designed to work.
I can't remember right off the top what the workflow is in Pro. I will post it later if I find it or remember.