Changing viewport shading with python
You have to specify which 3D view you want to access.

For example, this will iterate through all the areas in the current screen and set any 3D views to rendered shading:

import bpy

for area in bpy.context.screen.areas: # iterate through areas in current screen
    if area.type == 'VIEW_3D':
        for space in area.spaces: # iterate through spaces in current VIEW_3D area
            if space.type == 'VIEW_3D': # check if space is a 3D view
                space.viewport_shade = 'RENDERED' # set the viewport shading to rendered




Update

Alternatively, you could also do the following:

import bpy

area = next(area for area in bpy.context.screen.areas if area.type == 'VIEW_3D')
space = next(space for space in area.spaces if space.type == 'VIEW_3D')
space.viewport_shade = 'RENDERED'  # set the viewport shading


Instead of looping through the whole bpy.context.screen.areas, etc, the next() method returns area as soon as it finds area.type == 'VIEW_3D'. This doesn't really affect performance for this example but it's a better way of achieving the same result. Plus, it looks neater!