How to add a driver via python? I want to add a driver to the Z location of an object via python.
After reading the docs and this question, I would guess something like this:

bpy.context.object.driver_add("location", index=2)


However when run in the python console it says

Traceback (most recent call last):
  File "&lt;blender_console&gt;", line 1, in &lt;module&gt;
TypeError: driver_add() takes no keyword arguments
The error message gives a hint:

TypeError: driver_add() takes no keyword arguments

So pass the index as positional argument:

bpy.context.object.driver_add("location", 2)