4

As the title suggests, I'm trying to use a environment variable in a config file for a Flask project (in windows 10).

I'm using a virtual env and this far i have tried to add set "DATABASE_URL=sqlite:///models.db" to /Scripts/activate.bat in the virtualenv folder. But it does not seem to work. Any suggestions?

jst_swe
  • 51
  • 3

2 Answers2

0

Flask does not automatically take configuration variables from environment variables. You have to set them manually like so:

app.config.from_envvar('YOURAPPLICATION_SETTINGS')

More info regarding configuration can be found here.

To set an environment variable in Windows, you should do as described here:

setx DATABASE_URL=sqlite://something.something
Community
  • 1
  • 1
lesingerouge
  • 1,160
  • 7
  • 14
  • This is the code for reading the config `app.config.from_object('config.DevelopmentConfig')` and in the config file: `class DevelopmentConfig(BaseConfig) SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL']` This is the errormessage i get: `config.py", line 7, in BaseConfig SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL'] os.py", line 425, in __getitem__ return self.data[key.upper()] KeyError: 'DATABASE_URL'` – jst_swe May 05 '16 at 09:15
  • 1
    Yes, but from what i can see in the docs there is no information on how to set an environment variable in a virtualenv (windows) – jst_swe May 05 '16 at 09:37
0

The problem was that PyCharm does not activate the virtualenvironment when pressing the run button. It only uses the virtualenv python.exe.

jst_swe
  • 51
  • 3