【问题标题】:Python ImportError: No module named main in Google app engine projectPython ImportError:Google 应用引擎项目中没有名为 main 的模块
【发布时间】:2013-03-21 07:31:46
【问题描述】:

我有以下 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 = webapp2.WSGIApplication([('/.*', MainPage)],
                                     debug=True)

【问题讨论】:

    标签: python google-app-engine


    【解决方案1】:

    您调用了文件 index.py,而不是 main.py。要么重命名它,要么在 yaml 中使用index.app

    【讨论】:

    • 你是对的人,谢谢你对菜鸟感到抱歉......请我在 _LoadHandler 中出现另一个错误 raise ImportError('%s has no attribute %s' % (handler, name)) ImportError: 的模块 'main' 没有属性 app
    • 你需要去掉if __name__ = "__main__"的东西,以及def main()这一行,把东西放在模块级别的那个函数里面。
    • 请你解释一下“把东西放在模块级别的函数中”是什么意思?
    • @frank 看起来你跳过了教程.. :) developers.google.com/appengine/docs/python/… 确保你至少完成了一次直到最后..
    • @Lipis 我需要这个developers.google.com/appengine/docs/python/… 对吗?
    【解决方案2】:

    您遇到的问题是您的app.yaml 文件没有正确描述您的代码。这是有问题的地方:

    - url: .*
      script: main.app
    

    这表示所有与先前条目不匹配的 URL 应由 main 模块的 app 对象处理,该对象应为 WSGI 应用程序对象(请参阅 WSGI 标准)。

    这不起作用,因为您的代码设置不同。您的主要模块在index.pyindex 模块)中,它与服务器的接口是通过 CGI 标准(尽管内部使用 WSGI)。

    所以,你需要改变一些东西。它可以是应用程序的app.yaml 描述,也可以是您的代码组织。

    让您的代码像 CGI 风格的程序一样工作很容易。只需将app.yaml 更改为指向index.py 作为脚本。在这种情况下.py 部分是文件扩展名,文件将作为脚本运行。

    如果您想使用更新的、兼容 WSGI 的样式(这可能是最好的选择),the documentation 建议使用以下格式:

    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
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    相关资源
    最近更新 更多