How can I set the Lamp type using Python scripting?
Confusion between object and its data

Blender has many object types, 'LAMP' is one of them. It is set by the data type on creation and is read only.  

&gt;&gt;&gt; D.objects['Lamp']
bpy.data.objects['Lamp']

&gt;&gt;&gt; D.objects['Lamp'].type
'LAMP'

&gt;&gt;&gt; D.objects['Lamp'].type = 'LAMP'
Traceback (most recent call last):
  File "/usr/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "&lt;blender_console&gt;", line 1, in &lt;module&gt;
AttributeError: bpy_struct: attribute "type" from "Object" is read-only


A 'LAMP' type object (bpy.data.objects) has a lamp (bpy.data.lamps) object as its data. Just like a 'MESH' object has a mesh (bpy.data.meshes)  The type of lamp can be set on the data part.

&gt;&gt;&gt; D.objects['Lamp'].data
bpy.data.lamps['Lamp']

&gt;&gt;&gt; lamp = D.objects['Lamp'].data
&gt;&gt;&gt; lamp.type
'SPOT'

&gt;&gt;&gt; lamp.type = 'HEMI'
&gt;&gt;&gt; lamp.type
'HEMI'