from sqlalchemy import create_engine def create_connection(): db_name = get_env_variable("DB_NAME") db_user = get_env_variable("DB_USER") db_password = get_env_variable("DB_PASSWORD") db_url = get_env_variable("DB_URL") db_connection_uri = "mysql+pymysql://{user}:{pw}@{url}/{db}".format( user=db_user, pw=db_password, url=db_url, db=db_name ) engine = sqlalchemy.create_engine(db_connection_uri, echo=True) def get_env_variable(name): try: return os.environ[name] except KeyError: message = "Expected environment variable '{}' not set.".format(name) raise Exception(message)