Importing in functions instead of at the beginning or the script?
<p>Quick background: writing a module. One of my objects has methods that may or may not be successfully completed - depending on the framework used underneath my module. So a few methods first need to check what framework they actually have under their feet. Current way of tackling this is:</p>

<pre><code>def framework_dependent_function():
    try:
        import module.that.may.not.be.available
    except ImportError:
        # the required functionality is not available
        # this function can not be run
        raise WrongFramework 
        # or should I just leave the previous exception reach higher levels?

[ ... and so on ... ]
</code></pre>

<p>Yet something in my mind keeps telling me that doing imports in the middle of a file is a bad thing. Can't remember why, can't even come up with a reason - apart from slightly messier code, I guess.</p>

<p>So, is there anything downright wrong about doing what I'm doing here? Perhaps other ways of scouting what environment the module is running in, somewhere near <code>__init__</code>?</p>
