Python ImportError: No module named main in Google app engine project

最后都变了- 提交于 2021-02-07 19:07:14

问题


I have the following app.yaml file

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"

And the file 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)

The files that I should deploy are just html files or js or images, I get the following error after compiling the app:

raise ImportError('%s has no attribute %s' % (handler, name)) ImportError: has no attribute app


Solved: I had to call "app" not "application" !

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

回答1:


You've called the file index.py, not main.py. Either rename it, or use index.app in the yaml.




回答2:


The issue you are having is that your app.yaml file doesn't properly describe your code. Here is the offending bit:

- url: .*
  script: main.app

This says that all URLs that were not matched by some previous entry should be handled by the app object of the main module, which should be a WSGI application object (see the WSGI standard).

This doesn't work because your code is set up differently. Your primary module is in index.py (the index module) and its interface with the server is via the CGI standard (though WSGI is used internally).

So, you need to change something. It could either be the app.yaml description of the app, or it could be the organization of your code.

Making your code work as a CGI-style program is easy. Just change app.yaml to point to index.py as the script. The .py part in this case is the file extension, and the file will be run as a script.

If instead you want to go with the newer, WSGI-compatible style (which is probably the best option), the documentation suggests the following format:

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)])

Your code is almost like this already. To make it work, get rid of your main function and if __name__ == "__main__" boilerplate. Replace it with:

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

This creates an app object at the top level of your module. Now, either rename your index.py file to main.py, or change app.yaml to point to index.app. The .app part of this is different this time. Rather than a file extension, it represents Python member access (in this case, accessing a global variable in a module).



来源:https://stackoverflow.com/questions/15722882/python-importerror-no-module-named-main-in-google-app-engine-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!