21 Nov 2018
Usually, when you'd want to have a PIP library accessible to your web app project, you'd install the library on your server machine and every project on that server could access it.
GAE apps work a bit differently. Since a GAE app runs in a sandboxed environment, it cannot access any resources from the server it's running on.
That's why you need to include PIP libraries directly into your web app project.
You could do this manually by copy/pasting the library folder into your project. But instead, it's better to automate this process. Let us show you how.
libs
folderWithin the root of your GAE web app create a new folder called libs
(short for "libraries").
libs
into .gitignore
If you use GIT (and you should), make sure to add the libs
folder into the .gitignore
file, because it shouldn't eat up your GIT space.
requirements.txt
fileIf you've ever automated your PIP workflow, you're already familiar with this step. The requirements.txt
file will hold a list of your PIP libraries.
Let's say you'd like to install two PIP libraries, markdown2 and bleach. The most simple way is to include their names into the requirements.txt
file, one in each line:
markdown2
bleach
But if you'd like to have more control over which version exactly of the libraries you want to have, you can write version numbers next to the names:
markdown2==2.3.5
bleach==2.1.2
Now it's time to install your PIP libraries. As we said, the libraries must be included in the project folder. Let's see how the command looks like:
pip install -t libs -r requirements.txt
After you run this command, take a look into your libs
folder. You should see the newly installed libraries in it.
The process is not over yet. There's one last thing to do before you can start using the new libraries in your project.
appengine_config.py
You need to create a file called appengine_config.py
in the root of your project. In that file include the following code:
from google.appengine.ext import vendor
vendor.add('libs')
This will allow your GAE project to access the third-party libraries from PIP (the ones that you have installed in your project).
Now you can start using the newly installed libraries in your project. Congrats!
If you're using a continuous integration for your web app (and again, you should), make sure to include the pip install command (from step 4) in it.
You can upgrade your PIP libs with the following command:
pip install -t libs -r requirements.txt --upgrade
That's it! Hope this article helped you in your web app development and saved you some time and nerves. Make sure to subscribe to our newsletter below so you don't miss our future useful GAE-related articles.
Please ask questions or give other feedback via Reddit: r/AppEngine. You can also tag our official account by mentioning it in the comment/topic: u/GAEdevs.