Raise an exception at import if conditions not met
<p>I have a module that depends on some system settings. For example, to work properly it needs to have an environment variable FOO set. I would like the module to raise an Exception if this condition is not met at import time.</p>

<pre><code># mymodule.py
if 'FOO' not in sys.environ:
    raise SomeException('ensure that FOO is provided')
</code></pre>

<p>I would like to know:</p>

<ol>
<li>Is it the best practice to check those conditions at import time, or maybe it would be better to do it later (when)</li>
<li>What type of exception should I raise? Should it be my own <code>class MyModuleImportError(Exception)</code> or maybe some built-in exceptions are more suitable and commonly used? One candidate would be <a href="http://docs.python.org/2/library/exceptions.html#exceptions.ImportError" rel="nofollow">ImportError</a>, but it seems to be reserved for situation when python <em>"fails to find the module definition"</em></li>
</ol>
