|
| 1 | +========================== |
| 2 | +Frequently Asked Questions |
| 3 | +========================== |
| 4 | + |
| 5 | +Hopefully you can find an answer here to one of your questions. If not, please |
| 6 | +contact `help@twilio.com <mailto:help@twilio.com>`_. |
| 7 | + |
| 8 | +ImportError messages |
| 9 | +-------------------- |
| 10 | + |
| 11 | +If you get an error that looks like this: |
| 12 | + |
| 13 | +.. code-block:: python |
| 14 | +
|
| 15 | + Traceback (most recent call last): |
| 16 | + File "twilio.py", line 1, in <module> |
| 17 | + from twilio.rest import TwilioRestClient |
| 18 | + File "/Users/kevin/code/twilio-python/docs/twilio.py", line 1, in <module> |
| 19 | + from twilio.rest import TwilioRestClient |
| 20 | + ImportError: No module named rest |
| 21 | +
|
| 22 | +Check to make sure that you don't have a file named ``twilio.py``; Python will try to |
| 23 | +load the Twilio library from your ``twilio.py`` file instead of from the Twilio |
| 24 | +library. |
| 25 | + |
| 26 | +If you get an error that looks like this: |
| 27 | + |
| 28 | +.. code-block:: python |
| 29 | +
|
| 30 | + Traceback (most recent call last): |
| 31 | + File "test.py", line 1, in <module> |
| 32 | + import twilio.rest |
| 33 | + ImportError: No module named twilio.rest |
| 34 | +
|
| 35 | +Your Python installation cannot find the library. |
| 36 | + |
| 37 | +Check which versions of ``pip`` and Python you are running with this command in |
| 38 | +the Terminal: |
| 39 | + |
| 40 | +.. code-block:: bash |
| 41 | +
|
| 42 | + which -a python |
| 43 | + which -a pip |
| 44 | +
|
| 45 | +``pip`` needs to install the Twilio library to a path that your Python executable |
| 46 | +can read from. Sometimes there will be more than one version of pip, like |
| 47 | +pip-2.5, pip-2.7 etc. You can find all of them by running ``compgen -c | grep |
| 48 | +pip``. There can also be more than one version of Python, especially if you have |
| 49 | +Macports or homebrew. |
| 50 | + |
| 51 | +You also may be using an outdated version of the twilio-python library, which |
| 52 | +did not use a ``twilio.rest.TwilioRestClient`` object. Check which version of the |
| 53 | +twilio library you have installed by running this command: |
| 54 | + |
| 55 | +.. code-block:: bash |
| 56 | +
|
| 57 | + $ pip freeze | grep twilio # Or pip-2.7 freeze etc. |
| 58 | +
|
| 59 | +The latest version (as of January 2012) is 3.3. If you are running an outdated |
| 60 | +version, you can upgrade with this command: |
| 61 | + |
| 62 | +.. code-block:: bash |
| 63 | +
|
| 64 | + $ pip install --upgrade twilio |
| 65 | +
|
| 66 | +Note that if you have code that uses the older version of the library, it may |
| 67 | +breake when you upgrade your site. |
| 68 | + |
| 69 | +Formatting phone numbers |
| 70 | +------------------------ |
| 71 | + |
| 72 | +Twilio always returns phone numbers that are formatted in the `E.164 format |
| 73 | +<http://en.wikipedia.org/wiki/E.164>`_, like this: ``+12125551234``. However |
| 74 | +your users may enter them like this: ``(212) 555-1234``. This can lead to |
| 75 | +problems when, for example, Twilio makes a POST request to your server with the |
| 76 | +``From`` phone number as ``+12125551234``, but you stored the phone number in |
| 77 | +your database as ``(212) 555-1234``, causing a database lookup to fail. |
| 78 | + |
| 79 | +We suggest that you convert the number to E.164 format |
| 80 | +before you store it in the database. The `phonenumbers |
| 81 | +<https://github.com/daviddrysdale/python-phonenumbers>`_ library is excellent |
| 82 | +for this purpose. Install it like this: |
| 83 | + |
| 84 | +.. code-block:: bash |
| 85 | +
|
| 86 | + $ pip install phonenumbers |
| 87 | +
|
| 88 | +Then you can convert user input to phone numbers like this: |
| 89 | + |
| 90 | +.. code-block:: python |
| 91 | +
|
| 92 | + import phonenumbers |
| 93 | +
|
| 94 | + def convert_to_e164(raw_phone): |
| 95 | + if not raw_phone: |
| 96 | + return |
| 97 | +
|
| 98 | + if raw_phone[0] == '+': |
| 99 | + # Phone number may already be in E.164 format. |
| 100 | + parse_type = None |
| 101 | + else: |
| 102 | + # Assume it's a US number |
| 103 | + parse_type = "US" |
| 104 | +
|
| 105 | + phone_representation = phonenumbers.parse(raw_phone, parse_type) |
| 106 | + return phonenumbers.format_number(phone_representation, |
| 107 | + phonenumbers.PhoneNumberFormat.E164) |
| 108 | +
|
| 109 | + print convert_to_e164('212 555 1234') # prints +12125551234 |
| 110 | +
|
| 111 | +
|
0 commit comments