Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your biggest problem is the one mentioned by sotapme. If you don't actually run <code>app.py</code> as your top-level script, you don't have any code calling <code>app.main()</code>, and therefore nothing initializing the global.</p> <p>Beyond that, "how to reference the instance from another module" is very easy, if you know one simple thing:</p> <p>In Python, globals are namespaced.</p> <p>To put it more concretely, in your other modules, just <code>import app</code>, then access the global as <code>app.myapp</code>.</p> <p>Using the <code>__name__ == '__main__'</code> trick, as sotapme explained, means that you can <code>import app</code> as many times as you want without it running the <code>main()</code> function every time.</p> <p>In particular, when you run this:</p> <pre><code>python app.py </code></pre> <p>The Python interpreter will load <code>app.py</code> with its <code>__name__</code> set to <code>'__main__'</code>, so the <code>if</code> statement will trigger, which will cause the (module-level) global variable <code>myapp</code> to get set to <code>App({})</code>.</p> <p>Now, when <code>resource.py</code> does an <code>import app</code>, its <code>__name__</code> will be set to <code>app</code>, so the <code>if</code> statement will <em>not</em> trigger, so you will construct a new <code>App</code> and replace the global. From the code in <code>resource.py</code>, you can use <code>app.myapp</code>, and you will be accessing the same object that code in <code>app.py</code> sees as <code>myapp</code>.</p> <hr> <p>You also ask for help to pass config to the <code>App</code> constructor. I'm not sure what your problem is here. You're passing an empty <code>dict</code> as a config. If you have a different <code>dict</code> to pass, just use it. Instead of this:</p> <pre><code>myapp = App({}) # Could use help here, how to pass config to it </code></pre> <p>do this:</p> <pre><code>myapp = App(configdict) </code></pre> <p>If your problem is knowing how to get that <code>configdict</code>, that depends on where the information comes from.</p> <p>If you're trying to parse a user-editable config file, the <a href="http://docs.python.org/2/library/configparser.html" rel="nofollow"><code>configparser</code></a> module works fine for traditional .ini-style files, and the docs have links that explain how to handle some other popular formats.</p> <p>If you're trying to build up config information from a command line, see <a href="http://docs.python.org/2/library/argparse.html" rel="nofollow"><code>argparse</code></a>.</p> <p>If you want to allow environment variables to interact with either of the above (e.g., a <code>MYAPP_CONFIG</code> might tell your <code>configparser</code> code to load a different config file than normal, or a <code>MYAPP_CACHE_DIR</code> might provide a different default for the <code>--cachedir</code> command-line argument for <code>argparse</code>), you get the values in <a href="http://docs.python.org/2/library/os.html#os.environ" rel="nofollow"><code>os.environ</code></a>, but have to write your own code to do anything with them.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload