0

I have a heavy duty initialization function that I run in my flask app in production. I download a large file which has the latest data I am interested in.

However, this becomes a huge problem while developing this app. Whenever I make code changes in PyCharm, the reload activity calls this initialization function.

How can I make sure to run this function in production environment only, but not in development? I plan to add more complicated initialization logic that performs a host of other activities and checks before application launch. So, a trivial solution like commenting the function call is not what I'm looking for. I want to add some sophisticated logic that validates a lot of settings in productions.

timgeb
  • 73,231
  • 20
  • 109
  • 138
horatius
  • 694
  • 10
  • 28
  • Flask has a `FLASK_ENV` config setting specifically for this: https://flask.palletsprojects.com/en/1.1.x/config/#environment-and-debug-features – Aaron D Feb 17 '21 at 19:50
  • Thanks @AaronD. That's what I plan on doing with the solution suggested below. – horatius Feb 18 '21 at 19:14

1 Answers1

2

You could use an environment variable.

Either set one on the production system.

import os

if os.environ.get('DO_INIT'):
    initialize()

Or on your local system:

if not os.environ.get('SKIP_INIT'):
    initialize()
timgeb
  • 73,231
  • 20
  • 109
  • 138