Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import error in twilio

I am having the same problem as this thread regarding twilio-python:

twilio.rest missing from twilio python module version 2.0.8?

However I have the same problem but I have 3.3.3 installed. I still get "No module named rest" when trying to import twilio.rest.

Loading the library from stand alone python script works. So I know that pip installing the package worked.

from twilio.rest import TwilioRestClient


def main():
    account = "xxxxxxxxxxxxxxxx"
    token = "xxxxxxxxxxxxxxxx"
    client = TwilioRestClient(account, token)

    call = client.calls.create(to="+12223344", 
                               from_="+12223344", 
                               url="http://ironblanket.herokuapp.com/",
                               method="GET") 

if __name__ == "__main__":
    main()

but this does not work:

from twilio.rest import TwilioRestClient


def home(request):
    client = TwilioRestClient(account, token)

Do you have any idea what I can try next?

like image 567
David Dehghan Avatar asked Dec 04 '22 05:12

David Dehghan


2 Answers

I named a python file in my project twilio.py. Since that file was loaded first, then subsequent calls to load twilio would reference that file instead of the twilio library.

TLDR: just don't name your python file twilio.py

like image 107
David Dehghan Avatar answered Dec 28 '22 08:12

David Dehghan


Check which versions of pip and python you are running with this command:

which -a python
which -a pip

pip needs to install to a path that your Python executable can read from. Sometimes there will be more than one version of pip like pip-2.5, pip-2.7 etc. You can find all of them by running compgen -c | grep pip. There can also be more than one version of Python, especially if you have Macports or brew or multiple versions of Python installed.

Check which version of the twilio module is installed by running this command:

$ pip freeze | grep twilio          # Or pip-2.7 freeze etc.

The output should be twilio==3.3.3.

I hope that helps - please leave a comment if you have more questions.

like image 40
Kevin Burke Avatar answered Dec 28 '22 10:12

Kevin Burke