0

I wish to run the following command using the Twilio CLI from Python:

ngrok_cmd = "twilio phone-numbers:update "+ my_number " --sms url=https://localhost:5000"
os.system(ngrok_cmd)

The command works on the terminal, but doesn't if I try to do it through python. It keeps giving the following error:

sh: 1: twilio: not found

EDIT:

I tried this:

ngrok_cmd = "/home/pi/.config/nvm/versions/node/v16.13.1/bin/twilio phone-numbers:update "+ my_number " --sms url=http://localhost:5000"
os.system(ngrok_cmd)

now I get this error:

 » Could not find profile.
 » To create the profile, run:

  twilio profiles:create

Alternatively, twilio-cli can use credentials stored in environment variables:

# OPTION 1 (recommended)
export TWILIO_ACCOUNT_SID=your Account SID from twil.io/console
export TWILIO_API_KEY=an API Key created at twil.io/get-api-key
export TWILIO_API_SECRET=the secret for the API Key

# OPTION 2
export TWILIO_ACCOUNT_SID=your Account SID from twil.io/console
export TWILIO_AUTH_TOKEN=your Auth Token from twil.io/console

Once these environment variables are set, a twilio-cli profile is not required and you may skip the "login" step.

However, I have already set the environmental variables in /etc/profile and verified it with:

printenv | grep TWI

I do not know what is the cause of this error. Can anyone help me with this?

xyiong
  • 363
  • 1
  • 10
  • you run this from terminal? you run this through virtual environment? and if so you installed this package? – Bernana Dec 09 '21 at 08:07
  • @Bernana not virtual environment, running it on the raspberry pi os. I've followed this: https://www.twilio.com/docs/twilio-cli/quickstart and added my key and token to the raspberry pi as environmental variables. it works on the terminal but not through python... – xyiong Dec 09 '21 at 08:20
  • I just tried this on my laptop. I have the `twilio` CLI installed and ran `os.system("twilio")` and received the help text response. Is your python program running in the same environment that you installed the Twilio CLI to? – philnash Dec 09 '21 at 23:00
  • @philnash Thanks for helping! I tried Mark Setchell's solution below and got a new error... Any ideas why this could have happened? – xyiong Dec 12 '21 at 12:52
  • I see you have sorted it out now, thanks to Mark for the help! – philnash Dec 12 '21 at 23:22

1 Answers1

2

The error message means that your shell cannot find the twilio command. That is because it is looking in a different place from where it looks when you run it in your Terminal because the PATH (where shells look for commands) is not set the same.

You need to go in your normal Terminal where twilio does work and run:

which twilio         # or alternatively 
type twilio

That will tell you where the twilio command is, i.e. the full path to it.

Use that same full path in your Python code so that it can find it.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Hi, I tried doind this instead: ```os.system('/home/pi/.config/nvm/versions/node/v16.13.1/bin/twilio phone-numbers:update ' +my_number+ ' --sms-url=https://localhost:5000')``` but now it tells me that it cannot find my environment variables. I have confirmed that the environment variables exist with ```printenv | grep TWI```. How do I fix this? – xyiong Dec 12 '21 at 10:52
  • 1
    When you ran `printenv | grep TWI` you did that in your Terminal which has run your login profile. When Python runs your script it doesn't login and it doesn't set those environment variables. You can prove that by running `os.system("printenv")` from Python - it is a different environment. You need to source the script that sets the environment variables inside your `os.system()` before you use Twilio. – Mark Setchell Dec 12 '21 at 15:28
  • I see... If you do not mind, could you teach me how do I 'source the script that sets the environment variables' to Python? My environment variables are currently stored in ```/etc/profile```. – xyiong Dec 12 '21 at 17:33
  • 1
    Thank you for your help! I used this: https://stackoverflow.com/questions/40216311/reading-in-environment-variables-from-an-environment-file to 'source the script that sets the environment variables' and now it works! – xyiong Dec 12 '21 at 17:42