How can I add UV coords to a mesh from script?
Below you find a very well-performaning solution to set mesh UVs from a source in a per-vertex format to Blender's per-loop (face corner).

This assumes you have a sequence of float pairs (UV coord),
e.g. ((0.2, 0.1), (0.8, 0.9), (0.2,0.1), ...).

import bpy
from random import random

me = bpy.context.object.data

# Sample data
vert_uvs = [(random(), random()) for i in range(len(me.vertices))]

me.uv_textures.new("test")
me.uv_layers[-1].data.foreach_set("uv", [uv for pair in [vert_uvs[l.vertex_index] for l in me.loops] for uv in pair])


Original thread: Importing UV coordinates