2013-06-23 125 views
2

命名主要在我的谷歌App Engine應用程序,打算到URL /foo時,我得到的錯誤導入錯誤 - 沒有模塊GAE

ImportError: No module named main

。我應用程序中的所有文件都在父目錄中。

這是我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" 

這裏是我的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() 

我得到同樣的錯誤當我改變main.applicationmain.py或只是main。爲什麼會發生此錯誤?

回答

1

隨着documentation說,

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.

對我來說,問題是該行

upload: .* 

匹配的所有文件在我的父目錄,包括main.py.這意味着main.py不可用於動態處理程序。修復方法是將此行更改爲僅識別與此規則的URL行識別相同的文件:

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

看看python27入門。你混合CGI和WSGI。你必須在這裏使用webapp2。

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

參見這個博客帖子大約CGI和WSGI:http://blog.notdot.net/2011/10/Migrating-to-Python-2-7-part-1-Threadsafe

+0

謝謝,我應該使用webapp2。但是,它仍然不起作用。 [hello world example](https://developers.google.com/appengine/docs/python/gettingstartedpython27/helloworld)正常工作;當python腳本不是主要的請求處理程序時(即只爲'/ foo。*') –

+0

請解釋。你是什​​麼意思:「當python腳本不是主要的請求處理程序」 – voscausa

+0

因爲它只處理以'/ foo'開始的URL,而不是基本URL'/'。 –

4

你的配置是OK - 僅在main.py一個小失誤:你需要一個訪問application的名稱來自main模塊,因此配置爲:main.application。這種變化應該做的伎倆:

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

不要擔心 - application對象將不會上創建運行,也沒有從該模塊的進口,只會顯式運行的所有如.run_wsgi_app或谷歌的內部架構。

相關問題