0

I'm going through an online tutorial for building a Python/Flask app. Before I installed Postgres and messed with my database I could launch my app from command line no problem. Now, everytime I restart the command prompt I have to redefine the APP_SETTINGS and DATABASE_URL variables. The app works fine after I refine the variables, but I know this isn't the way its supposed to work. The errors are below:

 Traceback (most recent call last):
 File "app.py", line 12, in <module>
 app.config.from_object(os.environ['APP_SETTINGS'])
 File "C:\envs\acme\lib\os.py", line 423, in __getitem__
 return self.data[key.upper()]
 KeyError: 'APP_SETTINGS'

I'll then redefine the variable using "set":

set APP_SETTINGS=config.DevelopmentConfig

Here is the code I use for my app.py and config.py

config.py:

import os
# default config
class BaseConfig(object):
    DEBUG = False
    SECRET_KEY = 'secret key'
    SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL']

class DevelopmentConfig(BaseConfig):
    DEBUG = True

class ProductionConfig(BaseConfig):
    DEBUG = False

app.py

import os
app.config.from_object(os.environ['APP_SETTINGS'])

The same thing goes for the DATABASE_URL variable listed above in the config.py file. I also get a KEYERROR and must redefine the variable using "set" as well as type in my username and password below:

set DATABASE_URL=postgres://username:password@localhost/discover_flask_dev

I'm using Windows 8 and Python 2.7. I have these tools installed in my environment:

Flask==0.10.1
Flask-SQLAlchemy==2.0
Jinja2==2.7.3
MarkupSafe==0.23
SQLAlchemy==0.9.8
Werkzeug==0.9.6
gunicorn==19.1.1
ipython==2.3.0
itsdangerous==0.24
psycopg2==2.5.2
pyreadline==2.0

I'm assuming there is a basic method to solve my problem mentioned above. Please advice. Thanks!

1 Answer 1

1

Your key issue, it seems, is "everytime I restart the command prompt I have to redefine the APP_SETTINGS and DATABASE_URL variables".

From this superuser answer, it looks like there are two environment variable commands: SET (which reverts on command window close), and SETX (which persists after command window close).

Instead of
set DATABASE_URL=postgres://username:password@localhost/discover_flask_dev

Try using
setx DATABASE_URL "postgres://username:password@localhost/discover_flask_dev"

Note the slightly different syntax: SETX uses double quotes "..", and does not use equals =

Sign up to request clarification or add additional context in comments.

2 Comments

I did what you said and also started using Git Bash and the problem went away. Thanks!
Great! If you'd like to indicate that this answer solved the problem, please click the green checkmark to accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.