Minimize module usage

If a Python module is only needed in a single function, rather than a global
import, just import it for that function only.