简体   繁体   English

ImportError - GAE中没有名为main的模块

[英]ImportError - No module named main in GAE

In my Google App Engine app, I'm getting the error 在我的Google App Engine应用中,我收到了错误消息

ImportError: No module named main ImportError:没有名为main的模块

when going to the URL /foo . 什么时候去URL /foo All the files in my app are in the parent directory. 我的应用程序中的所有文件都在父目录中。

Here is my app.yaml : 这是我的app.yaml

application: foobar
version: 1
runtime: python27
api_version: 1
threadsafe: no

handlers:

- url: /foo.*
  script: main.application

- url: /
  static_files: index.html

- url: /(.*\.(html|css|js|gif|jpg|png|ico))
  static_files: \1
  upload: .*
  expiration: "1d"

Here is my main.py : 这是我的main.py

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

class Handler(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello world!') 

def main():
    application = webapp.WSGIApplication([('/foo', Handler)],
                                         debug=False)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()

I get the same error when I change main.application to main.py or just main . 当我将main.application更改为main.pymain时,我得到了同样的错误。 Why is this error occurring? 为什么会出现此错误?

Your configuration is OK - only for a small misstep in the main.py : you need an access of the application name from the main module, thus the config is: main.application . 您的配置没问题 - 仅适用于main.py的小错误:您需要从main模块访问application名称,因此配置为: main.application This change should do the trick: 这种改变应该成功:

application = webapp.WSGIApplication([('/foo', Handler)],
                                     debug=False)
def main():
    util.run_wsgi_app(application)

Don't worry - the application object will not run on creation, nor on import from this module, it will run only on explicit all such as .run_wsgi_app or in google's internal architecture. 不要担心 - application对象不会在创建时运行 ,也不会在从此模块导入时运行,它只会在显式所有内容上运行,例如.run_wsgi_app或google的内部架构。

Have a look at getting started with python27. 看看python27入门。 You mixing CGI and WSGI. 你混合了CGI和WSGI。 You have to use webapp2 here. 你必须在这里使用webapp2。

Your WSGI main.py : 你的WSGI main.py:

import webapp2

class Handler(webapp2.RequestHandler):

    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello World!')


application = webapp2.WSGIApplication([
    ('/foo', Handler),
], debug=True)

See also this blog post about CGI and WSGI : http://blog.notdot.net/2011/10/Migrating-to-Python-2-7-part-1-Threadsafe 另见这篇关于CGI和WSGI的博客文章: http//blog.notdot.net/2011/10/Migrating-to-Python-2-7-part-1-Threadsafe

As the documentation says, 正如文件所说,

Static files cannot be the same as application code files. 静态文件不能与应用程序代码文件相同。 If a static file path matches a path to a script used in a dynamic handler, the script will not be available to the dynamic handler. 如果静态文件路径与动态处理程序中使用的脚本的路径匹配,则该动态处理程序将无法使用该脚本。

In my case, the problem was that the line 在我的情况下,问题是该线

upload: .*

matched all files in my parent directory, including main.py. 匹配我父目录中的所有文件,包括main.py. This meant that main.py was not available to the dynamic handler. 这意味着main.py不可用于动态处理程序。 The fix was to change this line to only recognize the same files that this rule's URL line recognized: 修复是将此行更改为仅识别此规则的URL行识别的相同文件:

upload: .*\.(html|css|js|gif|jpg|png|ico)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM