How to include third party Python libraries in Google App Engine?
Actually I think this answer fits better here.
If you want to use 3rd party libraries that are not included in this list, then you'll have to add them manually.
In order to include manually any other library you have to have them inside the directory where the app.yaml
lives. So for example if you have the following structure:
hello
├── libs
│ └── bs4
├── hello.py
└── app.yaml
then in your hello.py
you have to put these two lines in the beginning of the file:
import sys
sys.path.insert(0, 'libs')
After doing that you'll be able to use any 3rd party library that you're going to put in that libs
directory.
For example:
from bs4 import BeautifulSoup
How do I manage third-party Python libraries with Google App Engine? (virtualenv? pip?)
(Jun 2021) This post is over a decade old, and so an updated answer is warranted now.
- Python 3: list 3P libraries in
requirements.txt
along with any desired version#s; they'll be automatically installed by Google upon deployment. (This is the same technique used if you decide to migrate your app to Google Cloud Functions or Cloud Run.) - Python 2 without built-in 3P libraries (regular 3P libraries):
- Create
requirements.txt
as above - Install/self-bundle/copy them locally, say to
lib
, viapip install -t lib -r requirements.txt
- Create
appengine_config.py
as shown in step 5 on this page
- Python 2 with built-in 3P libraries (special set of 3P libraries):
- All listed 3P libraries linked above are "built-in," meaning they're available on App Engine servers so you don't have to copy/self-bundle them w/your app (like in #2 above)
- It suffices to list them with an available version in the
libraries:
section of yourapp.yaml
like this - (Don't put built-in libraries in
requirements.txt
nor usepip install
to install them locally unless you want to self-bundle because, say if you need a newer version of the built-in library.) - Create
appengine_config.py
like the above.
If you have a Python 2 app with both built-in and non-built-in 3P libraries, use the techniques in both #2 and #3 above (built-in libraries in app.yaml
and non-built-in libraries in requirements.txt
and run the pip install
cmd above). One of the improvements in the second generation runtimes like Python 3 is that all these games with 3P libraries go away magically (see #1 above).
Example: Flask
Flask is a 3rd-party micro web framework, and it's an interesting case for this specific question. For Python 3, they all go into requirements.txt
, so you'd just add flask
to that file, and you're done. (Just deploy from there.)
For Python 2, it's even more interesting because it's a built-in library. Unfortunately, the version on App Engine servers is 0.12
. Who wants to use that when we're at/beyond 2.0.3
now?!? So instead of putting it in app.yaml
like other built-in libraries, you'd pretend the built-in version doesn't exist and put it in requirements.txt
then run pip2 install -t lib -r requirements.txt
to bundle/vendor it with your application code. (However, the final version for Python 2 is 1.1.4
, so that's what gets installed.)
Adding a third-party library (twilio) to project using Google App Engine and Django
My python lib's dir are two dir.
1) /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/...
2) /usr/local/lib/python2.7/...
My project points at 1), but pip install at 2)...
I tried at 1) ./pip install twilio
. and so, it works!
thanks.
Uploading Python third party libraries
What I did is created a file called fix_path.py in my root directory that looks like this:
import os
import sys
import jinja2
# path to lib direcotory
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'lib'))
Then I created a lib directory, and drop the module in there.
For example, I use WTForms. My file structure looks like this.
- lib
- wtforms
- fix_path.py
- somefile.py
when I am ready to call it from my somefile script
import fix_path # has to be first.
import wtforms
here is this example in my github source. checkout fix_path.py for setup and views.py for usage.
Django on Google App Engine- How to import/use 3rd party libraries?
There's a good guide on how to use Django on GAE here:
https://cloud.google.com/python/django/appengine
When you follow that guide you get a requirements-vendor.txt file. Edit that file and add django-geojson
and django-leaflet
to it.
After that you can run pip install -r requirements-vendor.txt -t lib/
to install them.
Now you can follow the rest of the guide for instructions on how to upload everything to GAE.
Related Topics
"Getaddrinfo Failed", What Does That Mean
Caesar Cipher Function in Python
Parsing Date/Time String with Timezone Abbreviated Name in Python
Pygame Already Installed; However, Python Terminal Says "No Module Named 'Pygame' " (Ubuntu 20.04.1)
How to Mock Requests and the Response
Converting Integer to Binary in Python
Difference Between Variables Inside and Outside of _Init_()
Access an Arbitrary Element in a Dictionary in Python
Python Functions Call by Reference
Send File Using Post from a Python Script
When Does Python Allocate New Memory for Identical Strings
How to Define a Threshold Value to Detect Only Green Colour Objects in an Image with Python Opencv