Python ImportError: 在 Google App Engine 项目中没有名为 main 的模块。

4
我有以下的app.yaml文件。
application: gtryapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:

- url: /images/(.*\.(gif|png|jpg))
  static_files: static/img/\1
  upload: static/img/(.*\.(gif|png|jpg))

- url: /css/(.*\.css)
  mime_type: text/css
  static_files: static/css/\1
  upload: static/css/(.*\.css)

- url: /js/(.*\.js)
  mime_type: text/javascript
  static_files: static/js/\1
  upload: static/js/(.*\.js)

- url: /(.*\.html)
  mime_type: text/html
  static_files: static/\1
  upload: static/(.*\.html)

- url: .*
  script: main.app


libraries:

- name: webapp2
  version: "2.5.2"

而文件app.py:

import webapp2

class MainPage(webapp2.RequestHandler):
def get(self):
    if self.request.url.endswith('/'):
        path = '%sindex.html'%self.request.url
    else:
        path = '%s/index.html'%self.request.url

    self.redirect(path)


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

我应该部署的文件只是 html 文件、js 或者图片,编译应用程序后我遇到了以下错误:

raise ImportError('%s 没有属性 %s' % (handler, name)) ImportError: 没有 app 属性


解决方案:我需要调用 "app" 而不是 "application"!

    app = webapp2.WSGIApplication([('/.*', MainPage)],
                                     debug=True)
2个回答

8
你把文件命名为index.py而不是main.py。要么改名,要么在yaml文件中使用index.app

你是对的,谢谢。抱歉我太菜了...请问,我在_LoadHandler中又遇到了一个错误: raise ImportError('%s has no attribute %s' % (handler, name)) ImportError: <module 'main' from '/Users/francescotangari/gtryapp/main.py'>没有属性app。 - FrankTan
@Lipis 我需要这个吗 https://developers.google.com/appengine/docs/python/gettingstartedpython27/staticfiles ? - FrankTan
@Frank,你需要按照教程的要求开始并完成它,作为一个新项目,然后再尝试将其应用到你现有的项目中。 - Lipis
好的,我写了这个:导入webapp2class MainPage(webapp2.RequestHandler): def get(self): if self.request.url.endswith('/'): path = '%sindex.html'%self.request.url else: path = '%s/index.html'%self.request.url self.redirect(path)application = webapp.WSGIApplication([('/.*', IndexHandler)], debug=True)但仍然无法正常工作。 - FrankTan
@Lipis 我仍然遇到了 ImportError: has no attribute app 的问题。 - FrankTan
显示剩余4条评论

2
您遇到的问题是您的app.yaml文件没有正确描述您的代码。以下是有问题的部分:
- url: .*
  script: main.app

这段话的意思是,所有未被之前匹配过的URL都应该由main模块的app对象处理,这个对象应该是一个WSGI应用程序对象(请参阅WSGI标准)。
但由于你的代码设置不同,所以这种方法行不通。你的主模块在index.py中(即index模块),并且它与服务器的接口是通过CGI标准实现的(虽然内部使用了WSGI)。
所以,你需要改变一些东西。可以修改app.yaml应用程序的描述,也可以重新组织你的代码。
将你的代码作为CGI风格的程序运行很容易,只需将app.yaml更改为指向index.py作为脚本即可。在这种情况下,.py是文件扩展名,文件将作为脚本运行。
如果你想采用较新的、兼容WSGI的样式(这可能是最好的选择),文档建议使用以下格式:
import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')

app = webapp2.WSGIApplication([('/', MainPage)])

你的代码已经非常接近正确的形式了。要让它工作起来,需要摆脱main函数和if __name__ == "__main__"的样板代码。请使用以下代码进行替换:

app = webapp.WSGIApplication([('/.*', IndexHandler)],
                              debug=False)

这将在您的模块顶层创建一个app对象。现在,要么将您的index.py文件重命名为main.py,要么更改app.yaml以指向index.app。这次.app的部分不同了。它不再是文件扩展名,而是表示Python成员访问(在本例中,访问模块中的全局变量)。


我仍然收到 ImportError: has no attribute app。 - FrankTan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接