1

Hello all: I have a basic program that copies a web page into a variable and checks the contents to see if a new press release was listed (i.e. by date since all PRs starts with a date in a consistent format. thanks to the help on this board it works.

Now I want to send a text message via twilio. I set my twilio account/number up and installed twilio according to these instructions. However, I get the error "ModuleNotFoundError: No Module named 'twilio'". I have uninstalled and re-installed several times. The only thing I can think of is that twilio is not installed where it should be. What are the file names and where should they be installed? Any advice would be appreciated.

===== RESTART: C:/Users/Family/Documents/Python Programs/check4newPRs.py 
June 5, 2017
found new press release dated June 5, 2017
Traceback (most recent call last):
  File "C:/Users/Family/Documents/Python Programs/check4newPRs.py", line 27, 
    in <module>
    import twilio
ModuleNotFoundError: No module named 'twilio'
>>> 

Here's my code:

# check4newPRs.py
import urllib.request

## Read web page contents into webPageCopy variable.
url = 'https://www.nwbio.com/press-releases/'
response = urllib.request.urlopen(url)
webPageCopy = response.read()

## Determining current date; populating month,day and variables
import datetime
import calendar
now = (datetime.datetime.now())
daynmbr = "05"  ## temporarily hard-coded the 5th of June, the last press released by comany. Need to remove prior to going live.

## Creating current date search string, used to search webpage. All press releases start with a date in the following format:  "June 5, 2017".
todaysdate = calendar.month_name[now.month] + " " + daynmbr.lstrip("0") + ", " + str(now.year)  ## Remove prior to going live.
##todaysdate = calendar.month_name[now.month] + " " + str(now.day).lstrip("0") + ", " + str(now.year)  ##use this one for production
print (str(todaysdate))

## ==========================================================================
## ======== press release is found, send Twilio sms text alert ========
## ==========================================================================

if (todaysdate.encode('utf-8'))  in webPageCopy:   ## todaysdate must be same byte type as the webPageCopy variable.
   print ('found new press release dated', todaysdate)

   import twilio
   import twilio.rest
   try:
       client = twilio.rest.TwilioRestClient(account_sid, auth_token)
       message = client.messages.create(
           body="New Northwest Biotherapeutics Press Release Found!!!",
           to="+1##########",
           from_="+1##########"
       )
   except twilio.TwilioRestException as e:
       print (e)
  • If you type `pip show twilio` does it return information and tell you what version you have installed? – Andy Jun 12 '17 at 14:36
  • Yes, it shows the name, version (6.3.0), summary, home-page, author, license (UNKNOWN), location (python36--32\lib\site-packages directory) – James Callaway Jun 12 '17 at 15:50
  • What happens if you open a python repl and try `from twilio.rest import Client`? – philnash Jun 12 '17 at 16:23
  • I am assuming a python "repl" is the python IDLE where you execute a program script. I created a empty script with just "from twilio.rest import Client" Apparently it worked with no error. The results were no error, just >>>>". – James Callaway Jun 12 '17 at 18:17
  • I uninstalled all versions and re-installed the 32-bit version. Now when I run the program, i get: Traceback (most recent call last): File "C:\Users\Family\Documents\Python Programs\check4newPRs.py", line 38, in client = Client(account_sid, auth_token) NameError: name 'account_sid' is not defined >>> – James Callaway Jun 12 '17 at 18:19
  • How stupid of me. The fields account_sid & Auth token need the info from my twilio account. Let me add that info and try it again. – James Callaway Jun 12 '17 at 18:28
  • Sweet!!! It worked!!!!!! Programming is really addictive. The positive feeling you get when you write a program that works is cool! I think I will find a class. – James Callaway Jun 12 '17 at 18:32
  • Thanks for all of you guys/gals help. – James Callaway Jun 12 '17 at 18:32

1 Answers1

1

Twilio developer evangelist here.

First, check which version of Twilio Python you have installed. If it is version 6, then it is the latest and you'll need to update your code.

When you want to use the Twilio REST API you should:

from twilio.rest import Client

Then you can create an instance of a Client and send a message like this:

client = Client(account_sid, auth_token)
message = client.messages.create(
    body="New Northwest Biotherapeutics Press Release Found!!!",
    to="+1##########",
    from_="+1##########"
)

Let me know if that helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • I have version 6.3. I changed my code and still get an error: from twilio.rest import Client ModuleNotFoundError: No module named 'twilio'. I really think the error has to do with some basic installation error. How does Python know where the Twilio api/dll/exe (or whatever) is? – James Callaway Jun 12 '17 at 16:19
  • Additional background Info: I am using Python's Integrated Development Environment (IDLE) version 3.6.1 (TK version 8.6.6). I save my python programs in a mydocs folder. Don't know if this matters... – James Callaway Jun 12 '17 at 16:27
  • By the way, I never said thank you, Philnash, for replying. – James Callaway Jun 12 '17 at 16:28
  • You might find you have multiple versions of Python installed, see this [SO question for some help](https://stackoverflow.com/questions/25968239/mac-python-import-error-no-module-named-site/25969007#25969007). – philnash Jun 12 '17 at 16:41
  • OK. I think I uncovered a problem: I have two versions of Python installed: Python 3.6 (32-bit) and python 3.6 (64-bit). The 32-bit version has a twilio folder in its "lib" directory. The 64-bit version doesn't. i changed the file associations of ".py" to use the 32.bit python version. but now i can't get into the IDLE editor. Once I find that, i'll test it. – James Callaway Jun 12 '17 at 16:59
  • wow Phinash, your good! You called it! I'll try to get all of this straight after o read the your link. I'll p[ost the results. Thanks a million. – James Callaway Jun 12 '17 at 17:01
  • Glad I could help! – philnash Jun 12 '17 at 17:04