Trolley_Theif
Trolley_Theif

Reputation: 69

importing Twilio on google app engine

I've seen other similar questions but none of the solutions are working for me. I am trying to get Twilio working with Google App Engine. I am using the python API and can't seem to get it to work. I tried a few tactics:

  1. used pip install twilio
  2. downloaded the twilio file directly into my root directory
  3. sym linked the required files according to a few tutorials

nothing seems to work. When I write the line "import twilio.twiml" it makes the google app engine crash and say "error: server error:

What is the best way to import Twilio and load into the google app engine server?

Upvotes: 0

Views: 330

Answers (3)

Trolley_Theif
Trolley_Theif

Reputation: 69

Thanks for the input. I had already tried all of these things. When I ran the app on a local host I saw in the console that the error I was facing was with 'pytz'

Turns out that Twilio requires the Pytz dependency to be in the root directory of Google App Engine. They have not updated the documentation yet.

Hope that helps anyone in the future.

Upvotes: 0

Jeffrey Godwyll
Jeffrey Godwyll

Reputation: 3893

You should use vendoring. The official documentation for Google App Engine with python also recommends using a virtualenv and installing third party libs into a subdirectory of the project root:

To install virtualenv:

$ sudo pip install virtualenv

Create a virtual environment somewhere outside your application directory. (These files should not be uploaded with your app code. Or you can create it in your project directory but will have to add a rule in your app.yaml to skip it.)

To create a virtual environment:

$ virtualenv <env_name>

$ source /path/to/<env_name>/bin/activate
$ cd <your_appengine_project_directory>
$ ln -s {virtualenv}/lib/python2.7/site-packages lib

This way a pip install in the virtualenv automatically goes to the lib directory as well.

$ pip install twilio

The docs then say to make an appengine_config.py file in the project root with the following content:

from google.appengine.ext import vendor
# Add any libraries installed in the "lib" folder.
vendor.add('lib')

This way a pip install in the virtualenv automatically goes to the lib directory as well.

You should thus be able to import twilio.twiml and take it on from there.

Upvotes: 0

Tim
Tim

Reputation: 43314

From the docs

..you will need to add Twilio's Python library to your application. This involves installing some package management tools and the latest version of the Twilio Python module, then symlinking the library and its dependencies into your Google App Engine application:

  • cd to the directory where your application is stored, this will be the helloworld directory you created if you followed the Getting Started guide.

  • Install setuptools (installation instructions).

  • Use setuptools to install pip:

    $ sudo easy_install pip
    
  • Use pip to install Twilio's Python library and dependencies:

    $ pip install twilio
    
  • Locate your site-packages directory. This is where pip installed the Twilio helper library:

    $ python -c "import site; print(site.getsitepackages()[0])"
    
  • Use the output of that command as the value of {SITE_PACKAGES} below to symlink the Twilio Python module to your application directory. First, link the Twilio library:

    $ ln -s {SITE_PACKAGES}/twilio .
    
  • Then link the Twilio Python module's dependencies:

    $ ln -s {SITE_PACKAGES}/httplib2 .
    $ ln -s {SITE_PACKAGES}/six.py .
    

You have now installed the Twilio library into your Google App Engine project.

You can then import twilio with for example

from twilio import twiml

Upvotes: 1

Related Questions