ExtractionError:无法将文件提取到egg缓存中

时间:2017-09-30 14:58:35

标签: python google-app-engine caching

我正在使用GAE在Windows上开发我的应用程序,但显示以下错误消息。

C:\Python27\Lib\site-packages

Perhaps your account does not have write access to this directory?  You can

change the cache directory by setting the PYTHON_EGG_CACHE environment

variable to point to an accessible directory.

所以我在这个网站上查了一些帖子,发现some articles,但我无法理解。我的理解是我需要在主目录中创建具有写访问权限的缓存目录,但我不明白“主目录”在哪里。

另外在其他帖子中,我找到答案说这种方法解决了我的问题。我使用pip卸载pyyaml,然后使用easy_install -z pyyaml

安装它

哪种方法是正确的?

import webapp2
import os
import jinja2
import cloudstorage
import mimetypes

from PIL import Image
from google.appengine.ext import ndb
from google.appengine.ext import blobstore
from google.appengine.api import users
from google.appengine.api import app_identity
from google.appengine.api import images
from models import Note
from models import CheckListItem
from models import NoteFile

jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

images_formats = {
'0':'image/png',
'1':'image/jpeg',
'2':'image/webp',
'-1':'image/bmp',
'-2':'image/gif',
'-3':'image/ico',
'-4':'image/tiff',
}

class MainHandler(webapp2.RequestHandler):
def get(self):
    user = users.get_current_user()
    if user is not None:
        logout_url = users.create_logout_url(self.request.uri)
        template_context = {
            'user': user.nickname(),
            'logout_url' : logout_url,
        }
        template = jinja_env.get_template('main.html')
        self.response.out.write(template.render(template_context))
    else:
        login_url = users.create_login_url(self.request.uri)
        self.redirect(login_url)

def post(self):
    user = users.get_current_user()
    if user is None:
        self.error(401)

    bucket_name = app_identity.get_default_gcs_bucket_name()
    uploaded_file = self.request.POST.get('uploaded_file')
    file_name = getattr(uploaded_file,'filename',None)
    file_content = getattr(uploaded_file,'file',None)
    real_path = ''
    if file_name and file_content:
        content_t = mimetypes.guess_type(file_name)[0]
        real_path = '/' + bucket_name + '/' + user.user_id() + "/" + file_name
        with cloudstorage.open(real_path,'w',content_type=content_t,options={'x-goog-acl':'public-read'}) as f:
            f.write(file_content.read())

    self._create_note(user, file_name, real_path)

    logout_url = users.create_logout_url(self.request.uri)
    template_context = {
        'user':user.nickname(),
        'logout_url': logout_url,
    }
    self.response.out.write(self._render_template('main.html',template_context))

def _render_template(self,template_name,context=None):
    if context is None:
        context = {}
    user = users.get_current_user()
    ancestor_key = ndb.Key("User",user.nickname())
    qry = Note.owner_query(ancestor_key)
    context['notes'] = qry.fetch()
    template = jinja_env.get_template(template_name)
    return template.render(context)


@ndb.transactional
def _create_note(self,user,file_name,file_path):
    note = Note(parent=ndb.Key("User", user.nickname()), title=self.request.get('title'), content=self.request.get('content'))
    note.put()
    item_titles = self.request.get('checklist_items').split(',')
    for item_title in item_titles:
        item = CheckListItem(parent=note.key, title=item_title)
        item.put()
        note.checklist_items.append(item.key)
    if file_name and file_path:
        url, thumbnail_url = self._get_urls_for(file_name)

        f = NoteFile(parent=note.key, name=file_name, url=url,thumbnail_url=thumbnail_url,full_path=file_path)
        f.put()

        note.files.append(f.key)
        note.put()

def _get_urls_for(self,file_name):
    user = users.get_current_user()
    if user is None:
        return
    bucket_name = app_identity.get_default_gcs_bucket_name()
    path = '/' + bucket_name + '/' + user.user_id() + '/' + file_name
    real_path = '/gs' + path
    key = blobstore.create_gs_key(real_path)
    try:
        url = images.get_serving_url(key, size=0)
        thumbnail_url = images.get_serving_url(key,size=150,crop=True)
    except images.TransformationError,images.NotImageError:
        url = "http://storage.googleapis.com{}".format(path)
        thumbnail_url = None

    return url,thumbnail_url



class MediaHandler(webapp2.RequestHandler):
def get(self,file_name):
    user = users.get_current_user()
    bucket_name = app_identity.get_default_gcs_bucket_name()
    content_t = mimetypes.guess_type(file_name)[0]
    real_path = '/' + bucket_name + '/' + user.user_id() + '/' + file_name
    try:
        with cloudstorage.open(real_path,'r')as f:
            self.response.headers.add_header('Content-Type',content_t)
            self.response.out.write(f.read())
    except cloudstorage.errors.NotFoundError:
        self.abort(404)

class ShrinkHandler(webapp2.RequestHandler):
def _shrink_note(self,note):
    for file_key in note.files:
        file = file_key.get() # this is the same as "file.get().url" in html file. we add the comment.
        try:
            with cloudstorage.open(file.full_path) as f:
                image = images.Image(f.read())
                image.resize(640)
                new_image_data = image.execute_transforms()

            content_t = images_format.get(str(image.format))
            with cloudstorage.open(file.full_path,'w',content_type=content_t) as f:
                f.write(new_image_data)
        except images.NotImageError:
            pass

def get(self):
    user = users.get_current_user()
    if user is None:
        login_url = users.create_login_url(self.request.url)
        return self.redirect(login_url)

    ancestor_key = ndb.Key("User",user.nickname())
    notes = Note.owner_query(ancestor_key).fetch()

    for note in notes:
        self._shrink_note(note)

    self.response.write('Done.')

app = webapp2.WSGIApplication([
(r'/', MainHandler),
(r'/media/(?P<file_name>[\w.]{0,256})',MediaHandler),
(r'/shrink',ShrinkHandler)
], debug=True)

1 个答案:

答案 0 :(得分:1)

看起来您正在开发一个标准的env GAE应用程序,但是您尝试在本地系统的站点包中安装(并使用它们)第三方库。这不起作用。

您需要在应用的库目录中安装第三方库(即使用-t <your_lib_dir>的{​​{1}}选项,您不需要特殊权限。另请参阅: