2013-03-30 20 views
3

我有以下app.yaml文件的Python導入錯誤:沒有模塊名爲在谷歌應用程序引擎項目主要

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'%(處理程序,名稱)) ImportError:has沒有屬性的應用程序


解決:我不得不打電話「應用程序」而不是「應用程序」!

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

回答

5

你已經調用了文件index.py,而不是main.py.重新命名它,或者在yaml中使用index.app

+0

你是對的人感謝你對於noobness抱歉...請問我在_LoadHandler中有另一個錯誤 raise ImportError('%s沒有屬性%s'%(handler,name)) ImportError: has沒有屬性的應用程序 –

+0

你需要去除'if __name__ =「__main __」'東西,以及'def main()'行,並且將這些東西放入模塊級別。 –

+0

請問你能解釋一下,「在模塊級別把這些東西放在那個函數裏面」是什麼意思? –

2

您遇到的問題是您的app.yaml文件沒有正確描述您的代碼。這是有問題的位:

- url: .* 
    script: main.app 

這說那不是由以前的一些條目匹配的所有URL應該由main模塊,這應該是一個WSGI應用對象的app對象進行處理(見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成員訪問(在這種情況下,訪問模塊中的全局變量),而不是文件擴展名。

+0

我編輯了問題 –

+0

我stil得到ImportError:沒有任何屬性的應用程序 –

+0

解決了因爲nooob而感到難過 –

相關問題