4

Google App engine documentation states that it is possible to upload and use third party libraries provided they written in pure Python.

What are the steps I need to take to do this?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
afroze
  • 173
  • 1
  • 4
  • 11
  • 1
    Just include them in your app engine project directory (or a subdirectory) and `import` them using the relative path. When you deploy to appengine, the subdirectory will be included in the push as well. – rjz May 18 '12 at 07:17
  • Thanks for the reply. So say that I want to include a library named "tweetstream" i just copy its folder in the app folder (which contains the app.yaml file) and in the application code just say "import tweetsream". Is that all? And wouldn't I be required to make changes in the app.yaml file for using third-party apps? – afroze May 20 '12 at 06:38

2 Answers2

11

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.

Busilinks
  • 1,724
  • 2
  • 13
  • 15
  • I do not understand what's the purpose of "fix_path.py" file? Also if I created the "lib" directory in the application root folder (which contains "app.yaml", would i need to mention path to lib directory then? – afroze May 20 '12 at 06:43
  • the fix_path file just routes to the lib directory where I store all my libraries. I don't make mention of the lib folder in my app.waml file. – Busilinks May 21 '12 at 14:12
  • Shouldn't this just be in the `__init__.py`? – Alden Jan 12 '15 at 06:18
0

Well I tried the same with following steps.

  1. created a directory(lib) with init file i.e lib/__init__.py in my project root.
  2. created my module (mymodule.py), and defined a function i.e.

    def myfunc():
        return "mycustomfunction"
    
  3. imported mymodule in my main.py

    from lib import mymodule
    

I could use the returned value from myfunc() and could pass that as a template value to my jinja2 template

Similarly, if we follow what @rjz also pointed out in the first answer, if the 3rd Party library is just a module then we can keep that in libs with an init file and it can be imported with an import statement ( point 3) . If the 3rd party library is a package then we can keep it in the project root and import it again with an import statement as this one in the main.py:

from thirdpartypackage import * 
jitendra
  • 11
  • 1