realpython / discover-flask

Full Stack Web Development with Flask.
http://discoverflask.com
4.51k stars 799 forks source link

Keyerror #10

Open Amanimasila opened 5 years ago

Amanimasila commented 5 years ago

Traceback (most recent call last): File "manage.py", line 9, in from solarpi.app import create_app File "/home/amani/Desktop/new/solarpi/solarpi/app.py", line 10, in from solarpi.settings import ProdConfig File "/home/amani/Desktop/new/solarpi/solarpi/settings.py", line 7, in class Config(object): File "/home/amani/Desktop/new/solarpi/solarpi/settings.py", line 8, in Config SECRET_KEY = os_env['SOLARPI_SECRET'] File "/home/amani/Desktop/new/.venv/lib/python2.7/UserDict.py", line 40, in getitem raise KeyError(key) KeyError: 'SOLARPI_SECRET'

i generated the key but still am getting the same error, can anyone help?

Amanimasila commented 5 years ago

setting file

import os

os_env = os.environ

class Config(object): SECRET_KEY = os_env['SOLARPI_SECRET'] APP_DIR = os.path.abspath(os.path.dirname(file)) # This directory PROJECT_ROOT = os.path.abspath(os.path.join(APP_DIR, os.pardir)) BCRYPT_LOG_ROUNDS = 13 ASSETS_DEBUG = False DEBUG_TB_ENABLED = False # Disable Debug toolbar DEBUG_TB_INTERCEPT_REDIRECTS = False CACHE_TYPE = 'simple' # Can be "memcached", "redis", etc. SQLALCHEMY_TRACK_MODIFICATIONS = False

class ProdConfig(Config): """Production configuration.""" ENV = 'prod' DEBUG = False DB_NAME = 'app.db' DB_PATH = os.path.join(Config.PROJECT_ROOT, DB_NAME) SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format(DB_PATH) DEBUG_TB_ENABLED = False # Disable Debug toolbar SENTRY_DNS = os_env.get('SENTRY_DNS', None)

class DevConfig(Config): """Development configuration.""" ENV = 'dev' DEBUG = False DB_NAME = 'dev.db' DB_PATH = os.path.join(Config.PROJECT_ROOT, DB_NAME) SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format(DB_PATH) DEBUG_TB_ENABLED = True ASSETS_DEBUG = True # Don't bundle/minify static assets CACHE_TYPE = 'simple' # Can be "memcached", "redis", etc.

class TestConfig(Config): TESTING = True DEBUG = True SQLALCHEMY_DATABASE_URI = 'sqlite:///dev.db' BCRYPT_LOG_ROUNDS = 1 # For faster tests WTF_CSRF_ENABLED = False # Allows form testing

RgnDunes commented 3 years ago

I'm not sure if I'm correct but I think you created your environment variable SOLARPI_SECRET in your local machine's environment but you are running your application through virtual environment venv you created so what this would do is it would search your environment variable SOLARPI_SECRET in your virtual environment but as your environment variable SOLARPI_SECRET is actually present in your local machine's environment it gives you error namely KeyError as it is unable to find SOLARPI_SECRET in your virtual environment variable.

RgnDunes commented 3 years ago

Refer this article to get a better insight of using virtual environment variables.

https://pypi.org/project/python-dotenv/