Object Rotation with polar angles python
To avoid any misunderstanding let me reiterate with this illustration.


Click to enlarge.

$\theta$ will control the orientation on the red axis.
$\phi$ will control the orientation on the blue axis.
If $\theta$ is a multiple of $\pi$, $\phi$ won't change the final orientation.

We can construct a quaternion from the $\theta$ and $\phi$ rotation and chain them. The mathutils.Quaternion class provides a rotate(other) function for this purpose.

import bpy
import math
import mathutils

θ = math.pi / 3
ϕ = math.pi / 2

q = mathutils.Quaternion((0, -1, 0), θ)
qϕ = mathutils.Quaternion((0, 0, 1), ϕ)

q.rotate(qϕ)

bpy.context.object.rotation_mode = 'QUATERNION'
bpy.context.object.rotation_quaternion = q


While this works, it doesn't really produce polar coordinates. Certain polar coordinates should be the same, and they currently aren't.



Calculation the spherical position

You can directly calculate the x, y and z values.

$x = \sin{\theta} \cos{\phi}$
$y = \sin{\theta} \sin{\phi}$
$z = \cos{\theta}$

Then you can generate a quaternion pointing to that position with the to_track_quat function.

position = mathutils.Vector((math.sin(θ) * math.cos(ϕ),
                             math.sin(θ) * math.sin(ϕ),
                             math.cos(θ)                ))

q = position.to_track_quat('-Z', 'X')


This will give you good control over which axis is oriented where.