How to import requests_toolbelt correctly in google app engine?

Viewed 2180

I am trying to import requests_toolbelt package in google app engine but keep getting import error. Already checked at https://toolbelt.readthedocs.io/en/latest/adapters.html#appengineadapte and https://cloud.google.com/appengine/docs/standard/python/issue-requests also gives same error.

It works fine on local but after deploying, the error appears: ImportError: No module named requests_toolbelt.adapters

I have this:

import requests
from requests_toolbelt.adapters import appengine

if not os.environ.get('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    appengine.monkeypatch()

requirements.txt has

requests
requests_toolbelt

2 Answers

Since you are using Python2.7 in the Standard environment, having the requests_toolbelt library in the requirements.txt file is not enough to upload it to App Engine, since it is not one of the Built-in Third-party Libraries.

To add it, you can follow this steps, as mentioned in the official documentation:

  • Run the following command:

    pip install -t lib -r requirements.txt

    This will install all the packages to the local environment, and copy them to the lib folder afterwards. Documentation on this.

  • Create the following file, named appengine_config.py:

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

    Note that this file needs to be in the same root path as the app.yaml, and the 'lib' string repersents the path from this root to the folder you created in the previous point.

  • Redeploy the application with gcloud app deploy

Once that is done, you should be able to run the application without errors related to the library.

As a side note, these steps are only a requirement in Python 2.7 in the Standard App Engine environment. In Python3 or in Flexible, having the libraries listed in the requirements.txt file is enough.

I was recommend also to add the "lib" folder to the IDE Interpreter.

In IDE of IntelliJ like PyCharm you go to settings -> project -> project interpreter -> click on settings small button -> show all

enter image description here

Then click on folders icon:

enter image description here

Then click on add icon:

enter image description here

Then pick your lib folder

enter image description here

Related