Tuesday, 10 March 2015

Updating Tweepy to v3.3.0 on Google App Engine.

The old version of Tweepy I was previously using was outdated and Twitter api had updated endpoints, namely, media_upload and update_status that accepted media_ids.

Here's what I did to update/install Tweepy v3.3.0 on GAE:
DISCLAIMER: These instructions are by no means the best way of doing this. Most steps were simply googled. These posts are more for self reference if I ever encounter the problem again.

I decided to write a post since the closest help I found online directly related to this issue was:
http://discuss.tweepy.org/t/tweepy-with-the-google-app-engine/146/2

Adding python libraries to appengine

You may want to create a new folder of thirdparty libraries that you will be adding very shortly if you haven't already.

Follow the instructions here:
http://stackoverflow.com/a/2354273

or a summarized version below:

Create a new directory in your root folder called /thirdparty.
Your root folder is where you would have your app.yaml file.
Inside /thirdparty, create an empty __init__.py file.

In your root folder, create a new file - i.e. fix_path.py
import os
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), "thirdparty"))
and in your main.py, add

import fix_path

before all other imports.

Installing Tweepy:

I cloned the tweepy into a new directory

git clone https://github.com/tweepy/tweepy.git
And copied the tweepy folder to /thirdparty  (i.e you would have /thirdparty/tweepy/api.py)

If you try running your dev server now, it will complain:
ImportError: No module named six

So go to https://pypi.python.org/pypi/six/ and download the tar file. Unzip it and copy the six.py file straight into your /thirdparty directory. (i.e. /thirdparty/six.py)

If that's all working, then the next error you'll see is:
ImportError: No module named requests
git clone git://github.com/kennethreitz/requests.git
Copy the requests folder into your /thirdparty folder. (i.e. you would have /thirdpary/requests/api.py)

If you see:
ImportError: No module named 'requests.packages.urllib3'
Try deleting some of the *.pyc files. Don't know if that was usual, but it worked for me.

Next error:
ImportError: cannot import name certs

Kind sir here solved this one for me:
https://groups.google.com/forum/#!msg/pythonforfacebook/7aOvlz6SvPg/wrkGSyK1ij0J

Add ssl to your app.yaml libraries. 

libraries:
- name: webapp2
  version: latest
- name: ssl
  version: latest

Moving on:
ImportError: No module named requests_oauthlib
git clone https://github.com/requests/requests-oauthlib.git
Copy the requests_oauth folder into your /thirdparty folder.

Next error, almost there:
ImportError: No module named oauthlib.common
git clone https://github.com/idan/oauthlib.git
Copy the oauthlib folder into your /thirdparty folder. (i.e. You would have /thirdparty/oauthlib/common.py)
and.. done.