Bpy.context.region_data always NonType

Unfortunately, this does not work either. In 2.79, it seems bpy.context.space_data is already NonType, so
1)

rv3d = context.space_data.region_3d

generates error

AttributeError: 'NoneType' object has no attribute 'region_3d'

or 2)

region = bpy.context.region
rv3d = bpy.context.region_data
ray_origin = region_2d_to_origin_3d(region, rv3d, point_px)

generates error

File "\Anaconda3\2.79\scripts\modules\bpy_extras\view3d_utils.py", line 93, in 
region_2d_to_origin_3d
   viewinv = rv3d.view_matrix.inverted()
AttributeError: 'NoneType' object has no attribute 'view_matrix'