Armature not in edit mode I have a script that creates an armature in the first run, and then finds it in the second run. When I try to add a bone to it in the second run, I get this error: Armature 'Armature' not in edit mode, cannot add an editbone.

if armature_name in bpy.data.objects.keys():
    armature = bpy.data.objects[armature_name]
    armature_data = armature.data
    print ('armature found')
else:
    bpy.ops.object.armature_add(location = (0.0, 0.0, 0.0))
    armature = bpy.context.object
    armature.name = armature_name
    armature_data = armature.data
    armature_created = True
    print ('armature created')

#I was in object mode before
bpy.ops.object.editmode_toggle()
#Also tried bpy.ops.object.mode_set(mode='EDIT')

#This crashes the second time
bone = armature_data.edit_bones.new(bone_name)


What am I doing wrong? Why am I not in edit mode? It seems that I am in edit mode, but the armature thinks I'm not.
It's more reliable to tell Blender which mode to switch to instead of relying on editmode_toggle(), because there's an enter editmode option, so you can't be sure what mode an object is in after creation:

bpy.ops.object.mode_set(mode='EDIT')


BTW: this is better than your current code:

try:
    armature = bpy.data.objects[armature_name]
except KeyError:
    # create armature