raise KeyError when using Twilio Python [duplicate]

Solution 1:

For OS X El Capitan, add environment variables:

  • open terminal, then

    cd ~/

    vi .bash_profile

once in vi editor editing the file, get into insert mode (press i) then add these two lines:

export TWILIO_ACCOUNT_SID="AC0123456789abcdefabcdefabcdefabcd"
export TWILIO_AUTH_TOKEN="0123456789abcdefabcdefabcdefabcd"

get into vi command mode (press escape) save and quit

:wq
  • reboot your computer

To check environment variables after reboot, open terminal, type printenv, you should see them on the list.

In your script, to use the environment variables, try this

import os

account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
auth_token  = os.environ.get('TWILIO_AUTH_TOKEN')

For configuring Windows environment variables take a look at my answer

Twilio Auth windows enviro variables