title: Flask app, global variable over multiple files
body: <p>I'm trying to share a variable I defined in my main function with a flask app which is imported from another file. First I tried to solve it via a classic <code>global</code> variable which did not bring me any further, until I stumbled over the concept of <a href="https://flask.palletsprojects.com/en/1.1.x/appcontext/#:%7E:text=Storing%20Data&amp;text=Flask%20provides%20the%20g%20object,being%20global%20within%20a%20context" rel="nofollow noreferrer">flask.g and the app context</a>.</p>
  <p>So I tried the following: I have two files in the same directory, a <code>main.py</code>:</p>
  <pre class="lang-py prettyprint-override"><code># main.py
  
  import app
  from flask import g
  
  if __name__ == &quot;__main__&quot;:
      with app.app.app_context():
          g.value = &quot;Hello World&quot;
      
      app.app.run(debug=True, threaded=True, host='localhost', port=5000)
  
  </code></pre>
  <p>and a <code>app.py</code>:</p>
  <pre class="lang-py prettyprint-override"><code># app.py
  
  from flask import Flask, g
  
  app = Flask(__name__)
  
  @app.route('/helloworld')
  def send_response():
      return g.value
  </code></pre>
  <p>However, when I request at http://localhost:5000/helloworld I get</p>
  <pre><code>AttributeError: '_AppCtxGlobals' object has no attribute 'value'
  </code></pre>
  <p>So it seems that setting the value <code>g.value</code> in one file is not reflected in the app.
  I'm a beginner in Flask and it is very likely I did not get the concept right.
  Similar questions did not get me any answer I could use to fix the issue:
  <a href="https://stackoverflow.com/questions/49226531/flask-passing-global-variable">Flask passing global variable</a>, <a href="https://stackoverflow.com/questions/35309042/python-how-to-set-global-variables-in-flask">python-How to set global variables in Flask?</a>, <a href="https://stackoverflow.com/questions/19277280/preserving-global-state-in-a-flask-application">Preserving global state in a flask application</a></p>
  <p>Help would be much appreciated!</p>