unable to fetch updated variable from an active python module
<p>I am new to python and trying to create a module that would fetch a specific variable from an active module preferably as read only. I have tried importing the file in test2 but the print statement displays the length as 0. Cant understand why it is not able to get the current status of the variable and only reading the initialization.</p>

<p>Below is what I have tried, any help would be greatly appreciated.</p>

<p>Thanks.</p>

<p>test1.py</p>

<pre><code>from datetime import datetime,timedelta
import time

data=[]
stop=datetime.now()+timedelta(minutes=5)
while datetime.now()&lt;stop:
        time.sleep(1)
        data.append(datetime.now().time())
</code></pre>

<p>test2.py:</p>

<pre><code>from test1 import *
print len(data)
</code></pre>

<p>When you wrap statements in:</p>

<pre><code>if __name__ == "__main__"
</code></pre>

<p>The code within only gets executed when you execute that specific module from the Python interpreter. So your Main function won't get executed, and consequently your data variable won't be initialized, unless you run:</p>

<pre><code>python test2.py
</code></pre>

<p>You can actually just remove that clause and it will work as expected.</p>

python