"ImportError: No module named twilio.rest"

Tomislav picture Tomislav · Feb 3, 2016 · Viewed 27.3k times · Source

I have installed python 2.7.10 with PATH access and correctly installed twilio. However, when I try to execute a code I get this error message

Traceback (most recent call last):
  File "C:\Users\tmslvo\Google Drive\Desktop\send text.py", line 1, in <module>
    from twilio.rest import TwilioRestClient
ImportError: No module named twilio.rest

Now I read that a reason might be that python can't find the twilio package so I tried the

which -a python
which -a twilio

commands (in my Windows command prompt) in which case I get

'which' is not recognized as an internal or external command,
operable program or batch file.

Does anybody have an idea of what I'm doing wrong?

Thank you!

Answer

Marcos Placona picture Marcos Placona · Feb 3, 2016

Twilio developer evangelist here.

I think your problem will be that somehow when you installed the library, it failed silently(?). A few things to keep in mind:

  1. When installing Python libraries, always make sure you use pip.
  2. Also, check that none of your files within the project are actually called twilio.py as this will conflict with the actual library.
  3. Check that you're using the version of Python you think you're using by running python --version

All that failing, run the installation again, and all going well (without errors), you should be able to test it quickly with the following code.

import twilio
import twilio.rest

try:
    client = twilio.rest.TwilioRestClient(account_sid, auth_token)

    message = client.messages.create(
        body="Hello World",
        to="+14159352345",
        from_="+14158141829"
    )
except twilio.TwilioRestException as e:
    print e