如何在appengine app中使用/usr/local/lib/python2.7/dist-packages中的模块

时间:2013-06-21 04:30:56

标签: python google-app-engine

首先让我说我不认为这实际上可行,但是,在我放弃努力吸引这个库之前,我想确定一下。所以我用easy_install添加了一个我在Github上找到的API库。我希望在我的appengine app中使用它。我写了一个简单的测试处理程序,它将创建模块主类的一个实例并打印一个请求。我的dev_appserver将启动但不会加载MainPage。 任何建议表示赞赏!

错误是:

ERROR    2013-06-21 04:24:00,450 wsgi.py:219] 
Traceback (most recent call last):
  File "/home/devin/google_appengine/google/appengine/runtime/wsgi.py", line 196, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/home/devin/google_appengine/google/appengine/runtime/wsgi.py", line 255, in     _LoadHandler
    handler = __import__(path[0])
  File "/home/devin/Projects/appengine/hackTheMidwest/perfectpet4me.py", line 4, in <module>
    import petfinder
ImportError: No module named petfinder
INFO     2013-06-21 04:24:00,455 server.py:593] default: "GET / HTTP/1.1" 500 -

这是我的主文件的代码:

import os
import urllib

import petfinder # THE INSTALLED LIBRARY

from google.appengine.api import users

import jinja2
import webapp2


JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=['jinja2.ext.autoescape'])

class MainPage(webapp2.RequestHandler):

    def get(self):
        template = JINJA_ENVIRONMENT.get_template('templates/index.html')
        self.response.write(template.render())

class TestPage(webapp2.RequestHandler):

    def get(self):
        # Instantiate the client with your credentials.
        api = petfinder.PetFinderClient(api_key='#####', api_secret='#####')
        pet = api.pet_getrandom()
        self.response.write(pet['name'])


application = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/test', TestPage),
], debug=True)

我的app.yaml:

application: ASDFASDFASDF
version: 1
runtime: python27
api_version: 1
threadsafe: true

- url: /.*
  script: perfectpet4me.application

libraries:
- name: webapp2
  version: latest
- name: jinja2
  version: latest

2 个答案:

答案 0 :(得分:2)

你做不到。将您的第三方库包含在应用程序的目录中,并在应用程序中使用它。它将在应用程序引擎中部署时与您的应用程序一起上传。

答案 1 :(得分:2)

这是我为谷歌应用引擎制作的gaenv工具的确切目的。 这是一篇博客文章,了解更多详情: http://blog.altlimit.com/2013/06/google-app-engine-virtualenv-tool-that.html

但这是摘要: 你安装它:

pip install gaenv
cd /to/your/project
gaenv

确保将您的第三方软件包放入requirements.txt并安装,它将创建一个符号链接,以便随附上传,因为appengine appcfg遵循符号链接。

您还可以在github上阅读有关一切如何工作的代码: https://github.com/faisalraja/gaenv

相关问题