Forums

issue with environment variable

Hi Python friends,

I have an issue with my environment variable. The .env file is in the root folder (worksample) and the code is in worksample/work_ex/dash I followed the instructions here: https://help.pythonanywhere.com/pages/environment-variables-for-web-apps/

My WSGI file looks like this:

# This file contains the WSGI configuration required to serve up your
# web application at http://<your-username>.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# The below has been auto-generated for your Flask project

import sys
import os
from dotenv import load_dotenv
project_folder = os.path.expanduser('~/worksample')
load_dotenv(os.path.join(project_folder, '.env'))

# add your project directory to the sys.path
project_home = '/home/worksample/work_ex'
if project_home not in sys.path:
sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
from dash_app.main import app
application = app.server
# from flask_app import app as application  # noqa

And in the code of my project, I have the following lines:

import os
KEY = os.getenv("KEY")
holidays = requests.get(f"https://calendarific.com/api/v2/holidays?&api_key={KEY}&country={country_code}&year=2023").text

Did I miss something?

Thank y'all for your help

Make sure that you have defined KEY in ~/worksample/.env and that you have reloaded your web app.

Hi glenn, thanks for your answer. I did that and it still doesn't work. Do you have a next best guess? Thanks again

What kind of errors do you see at the bottom of your web app's error log? Are they related to the missing KEY issue?

When I use the key in plain text, it works correctly. When I use an environment variable, I have the following error:

2023-03-29 17:27:09,819: Exception on /_dash-update-component [POST]

Traceback (most recent call last):

File "/home/worksample/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 2525, in wsgi_app

response = self.full_dispatch_request()

File "/home/worksample/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 1822, in full_dispatch_request

rv = self.handle_user_exception(e)

File "/home/worksample/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 1820, in full_dispatch_request

rv = self.dispatch_request()

File "/home/worksample/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/flask/app.py", line 1796, in dispatch_request

return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)

File "/home/worksample/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/dash/dash.py", line 1274, in dispatch

ctx.run(

File "/home/worksample/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/dash/_callback.py", line 440, in add_context

output_value = func(*func_args, **func_kwargs)  # %% callback invoked %%

File "/home/worksample/work_ex/dash_app/main.py", line 78, in data

data2 = result_js['response']['holidays']

TypeError: list indices must be integers or slices, not str

One of those (result_js['response'] or result_js['response']['holidays']) is a list instead of a dictionary, so you cannot use a string to access an element in it. Check the json that you're getting back and modify your code so that you are accessing the data correctly.

Thanks Glenn for your answer.

Yes that happens because when I use the environment variable, the api answer is an error message and then there is no holidays field in response.

That confirms there is an issue with environment variable

You can see the API answer here:

{'meta': {'code': 401, 'error_type': 'auth failed', 'error_detail': 'Missing or invalid api credentials. See https://calendarific.com/api for details.'}, 'response': []}

The most likely causes are either

  • you are not defining the variable in the env file
  • you are not reading the correct env file. Make sure that you have your env file at /home/worksample/worksample/.env

Thanks! It was the second option, due to a misunderstanding about os path expanduser. All good now, thanks again

Glad to hear that!

I'm having the same problem as worksample but I am pretty sure my .env file is in the correct place.

I am able to verify the app is getting them, because I set one as the text to be returned by visiting the '/' route and it displayed correctly.

the .env file is at /home/mikefress/mysite/.env in the same folder as the flask_app.py itself.

What have I missed?

Thanks for your help!

Did you use absolute path in your code?