22

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!

Tomislav
  • 233
  • 1
  • 3
  • 6

14 Answers14

53

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
Trenton
  • 11,678
  • 10
  • 56
  • 60
Marcos Placona
  • 21,468
  • 11
  • 68
  • 93
  • 26
    Point 2 helped me. After I renamed file from `twilio.py` to other name, it works. – Max Peng Jan 26 '17 at 09:32
  • 2
    I am still struggling with the same issue. I am using python version 2.7.13 and installed twilio via pip install twilio. It says "Successfully installed twilio-5.7.0". When I run the sample code, I get the error "No module named twilio". – iosCurator Feb 24 '17 at 03:21
  • 2
    +1. Had a source dir called twilio, and issued from twilio.utils import some_func. Renaming it to another name fixes the issue. – Yash Sharma Feb 01 '18 at 11:59
  • I think your pip is not configured properly . You may be getting succefuuly installed message but it is not install where it should be. try pip install --user i am sure it will work for you. pip install will work fine only n virtualenvironment. – Arun Apr 18 '18 at 16:03
  • Yep, I had a test file called `twilio.py`. I renamed it to `twilio-test.py` and everything worked. – allardbrain Feb 10 '21 at 06:17
  • It should be noted in #2 you may also not name your script with a twilio prefix either. I initially had twilio-sample.py and renaming to twillz.py resolved. – Russ Jun 10 '21 at 17:09
  • point two it help me. – Oscar Rangel Jul 17 '22 at 17:51
  • Thanks, the point 2 helped me!!. =) – VIX Jun 13 '23 at 19:20
10

I had this problem as well.

In my case, I had named my file twilio.py and that is what caused the error.

Renaming the file to send_sms.py ( or any other name of your choice) will resolve the issue!

Nick
  • 3,454
  • 6
  • 33
  • 56
9

try this: sudo pip3 install twilio --upgrade

hanumanDev
  • 6,592
  • 11
  • 82
  • 146
2

Close and then relunch all IDLE instances.

This sounds obvious but it worked for me, since the installations of components were successful

Vizz85
  • 195
  • 1
  • 13
2

I ran into this same issue. I had used easy_install instead of pip to install twilio which was the problem. To fix this I ran pip uninstall twilio and reinstalled using pip.

Dave Justen
  • 151
  • 5
1

rename file name other than twilio.py EX:send_sms.py

1

A bit late to the party here but I also ran into this issue. After some trial and error, it looks like it was due to the pip version I was using. I originally used - pip3 install twilio.

Now I'm unsure of the underlying reason why this did not work, but it seems that pip3 does not encompass all versions of python 3.x? Using pip3 list and pip3.8 list I noticed I had the twilio module for pip3 but not for pip 3.8.

I used the following and was able to solve the issue pip3.8 install twilio. I used pip3.8 because that matched the python3.8 version that I am using.

jayveesea
  • 2,886
  • 13
  • 25
0

Pycharm user:

Macs (mid 2017) come with python 2.6 and 2.7 installed. PyCharm uses by default 2.6. When you install twilio (Pip install) the module is installed in python version 2.7. So, when you try to run twilio from PyCharm you get

ImportError: No module named twilio.rest

Solution: change the python interpreter in PyCharm. Go to preferences > project interpreter and from the drop menu Project Interpreter choose python 2.7

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
0

I think your pip is not configured properly . You may be getting succefuuly installed message but it is not install where it should be. try pip install --user i am sure it will work for you. pip install may work fine only in virtualenvironment without any config.Try pip install --user package name

@iosCurator

Arun
  • 3,440
  • 1
  • 11
  • 19
0

I had first installed twilio with the easy_intall tool

I followed the steps below:

  • Uninstall twilio with the command pip uninstall twillo
  • Install twilio with the command pip install twilio
  • Close the python IDLE and relaunch it.
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0

For the windows user,

I have suggested, pip3 install twilio

Senthuja
  • 520
  • 1
  • 7
  • 19
0

Follow these steps (on mac):

  1. Shift + Command + P
  2. search: Configure Language Specific Setting
  3. search: Python
  4. add: "code-runner.runInTerminal": true

That's it!

Ask me any question about it by: My LinkedIn

ItSNeverLate
  • 533
  • 7
  • 8
0

I ran into this issue when using poetry for my dependency management. Poetry doesn't recognise it as an existing package yet, hence it won't run your code unless you try the poetry+pip way.

-2

there will be 2 reasons for this

1.make sure you kept right path for python files in environment location

2.install twilio commands: 1.pip3 install twilio (or) pip install twilio 2.python otpv.py