importing a module and altering the module behavior at import
<p>I want to import a module, but the import may behave differently depending on some external condition I want to impose. What are the strategies available to achieve this result ?</p>

<p>example. I want a module foo.py. If I <code>import foo</code> I get a print of "hello" or "goodbye" depending on some external condition which is independent on the module, and is instead dependent on external factors. A trivial one may be a global variable, but I don't think the python scoping rules allow me to get global variables from outside the module foo.</p>

<p>Example:</p>

<p>fop.py</p>

<pre><code>import __main__
try:
    __main__.bar
    present = True
except:
    present = False

if present:
    print "present"
else:
    print "not present"
</code></pre>

<p>Now, when I import the module, I can obtain different results</p>

<pre><code>Python 2.7.1 (r271:86832, Feb 27 2011, 20:04:04) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import foo
not present
&gt;&gt;&gt; 

Python 2.7.1 (r271:86832, Feb 27 2011, 20:04:04) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; bar = 5
&gt;&gt;&gt; import foo
present
</code></pre>

<p>I know it's weird, but I have a very, very, very good reason to do it.</p>
