Is it possible to check for global variables in IPython when running a file?
<p>I have a file like so:</p>

<pre><code>import pandas a pd

def a_func():
    print 'doing stuff'

if __name__ == "__main__":
    if 'data' not in globals():
        print 'loading data...'
        data = pd.read_csv('datafile.csv')
</code></pre>

<p>When I run the file in IPython with <code>run file.py</code>, it always loads the data, but when I print <code>globals.keys()</code> in IPython, I can see the <code>data</code> variable.  Is there a way to access the global variables from IPython from within my <code>file.py</code> script, so I don't have to load the data every time I run the script in IPython?</p>

<p>Everytime a python file is executed the globals() dictionary is reset by the interpreter. So if you will try to do something like </p>

<pre><code>print globals().keys()
</code></pre>

<p>you can see that 'data' is not in globals. This dictionary gets updated as the program runs. So I don't think you can refer to the globals() of the IPython in the program.</p>

<p>check this <a href="https://docs.python.org/2/faq/programming.html#how-can-i-have-modules-that-mutually-import-each-other" rel="nofollow">link</a> ,according to it, globals are emptied.</p>

python ipython