imrich
imrich

Reputation: 383

os.environ.get("key") returns none; hardcoding "key" works

Building a python 3 web app using flask which includes google maps.

Checking for API Key before loading index.html always raises RuntimeError:

if not os.environ.get("key"):
    raise RuntimeError("key not set")
return render_template("index.html", key=os.environ.get("key"))

Also tried os.getenv - the same problem occurs. Changing variable name does not solve the issue either.

Exported the variable to environment via export key=value and printenv returns correct value of key.

Hardcoding the API Key works and returns the map successfully:

return render_template("index.html", key=value)

Any ideas how to solve this?

Upvotes: 0

Views: 1728

Answers (1)

imrich
imrich

Reputation: 383

SOLVED: make sure to run the export var command in the same terminal window as flask run.

ALTERNATIVE: create websiteconfig.py file with key="value" and include import websiteconfig in your application. source: link

Upvotes: 1

Related Questions