How to execute some code and have data global available to modules on import
<p>I'm trying to build a package and what I would like is that on import some code would parse some data from a set of files and then have some dictionaries available to my modules. I was wondering what the best way to do something like this would be. Thanks for the help.</p>

<p>So if my package is:</p>

<pre><code>Package
    __init__.py
    *maybe other files*
    module1
        __init__.py
        foo.py
    module2
        __init__.py
        bar.py
</code></pre>

<p>How could I make it so that when I import Package, some set up code will run that reads some files and generates some dictionaries that would then be available to foo.py or bar.py, etc.</p>

<pre><code># run.py File where you are trying to display variable from other "global" module
import imp


def load_module(myglobals, path):
    with open(path, 'rb') as f:
        return imp.load_source(myglobals, path, f)


filename_path = '/home/turkus/myglobals.py'
module = load_module('myglobals', filename_path)
print module.CONSTANT


# myglobals.py (located in /home/turkus/myglobals.py
CONSTANT = 'something'
</code></pre>

<p>Then run:</p>

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

python