Clean up superflous imports and unnecessary inline imports

There are a number of places where we import standard library (and in some cases numpy) modules inline in functions.  As a general rule inline imports should be avoided.  Sometimes they are necessary (though in some cases might indicate that some refactoring may be desirable), but when it comes to stdlib imports it's definitely unnecessary.  For now I didn't touch many inter-package imports.

In particular I focused on modules that are imported *no matter what* in general when doing an `import astropy`, so there's no argument to be made that "Well, I'm deferring import of that module until it's *really* necessary."  It's irrelevant if `import astropy` will almost always lead to those modules being imported.  So the inline imports only serve to slow down the functions that they're in.

This also made it possible to `import astropy` from within a source checkout on Python 3 at least far enough to raise an error that running astropy from the source won't work on Python 3.  At the time this seemed like a related change, but I could separate it out to a separate PR if desired.