Forums

Code not running in tasks with "KeyError".

I have a script in tasks that runs perfectly fine when I am not using environment variables. It runs in bash console fine and runs in tasks.

I have now added environment variables via bash console like below...

export TwilioAccountSID=MYACCOUNTID

These have added fine, and I can see them when running env inside of the bash console. The code runs fine in bash and executes perfectly.

They are within the script like this...

TwilioAccountSID = os.environ.get("TwilioAccountID")

The problem I have is when I now setup a task for this script, it throws a KeyError.

Within the tasks command line I have added the environment variables like this...

export TwilioAccountSID=MYACCOUNTID; python3 myscript.py

I get a KeyError when doing this, and when just running:

python3 myscript.py

Can anyone help me to understand how to make this work?

I followed this from another thread...

https://help.pythonanywhere.com/pages/environment-variables-for-web-apps/

I don't fully understand that article (new to Python), but attempted to follow it.

I have setup a .env file in the same directory as my script. Added my environment variables in there. I have then installed the dotenv library via bash console, and imported the load object.

I also added this to my script...

from dotenv import load_dotenv

project_folder = os.path.expanduser('mydirectory')  # adjust as appropriate
load_dotenv(os.path.join(project_folder, '.env'))

But now I do not know what to do to get this to work in tasks?

Any help is much appreciated.

As a side note, I did add this line to the command in the task scheduler like so:

set -a; source ~/.env; set +a python3 myscript.py

And this has run successfully as per the logs, but the code hasn't executed. No SMS's were sent.

I am now completely stuck as to what to do next? Any help or direction on what I am doing wrong is much appreciated.

Could you double check what kind of errors (if any) do you get in your task log? Also -- you could add extra logging to the script to see if env variables are being set correctly or not (I guess you have something like my_var = os.getenv("MY_VAR") in your script, so you can add print(my_var, flush=True) to the script after that).

Thanks for the response.

So, added the print statements under the environment variables, and they just print out the correct keys I have added in the bash console when run. In the task log, they don't print anything? Not sure if that is expected as I am new to running tasks and Python in general.

For reference the bash console runs fine and the code executes how it should do and SMS messages are received.

I just ran the task twice...

Once with the command as...

python3 myscript.py

This returned this error:

proxy_client = TwilioHttpClient(proxy={'http': os.environ['http_proxy'], 'https': os.environ['https_proxy']})
  File "/usr/local/lib/python3.9/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'http_proxy'

Tried with this within the task command:

set -a; source ~/.env; set +a python3 myscript.py

And the code seems to run as I get a code 0 in the logs with no errors. But no SMS messages are received so the code doesn't execute as it does in the bash console.

Hopefully this makes sense.

Given that you have a paid account, I don't think you need to be accessing the http_proxy environment variable, which is only needed in free accounts, and won't be set in a paid account. You can just use code like this:

account_sid = 'your account id here'
auth_token = 'your twilio token here'

client = Client(account_sid, auth_token)

...there's no need to set up a TwilioHttpClient object.

Ok thanks. I'll change that.

Does everything else look good as far as the code and the way I have setup the environment variables within the bash console, and then with the command line code I used within the tasks too? Does all of that look correct or is there anything I may be missing?

I'd say -- if it works, it's OK; if not, let us know what the errors are and we'll try to push it forward.