2016-10-04 84 views
0

這是簡單的代碼來測試html.and中的重定向,我使用Spyder編寫Python代碼。GAE有效,但導入webapp2失敗Spyder

import webapp2 
from valid_day import valid_day 
from valid_month import valid_month 
from valid_year import valid_year 
from html_escape import escape_html 
form = """ 
<form method="post"> 
    What is your birthday? 
    <br> 
    <label> 
     Month 
     <input type="text" name="month" value="%(month)s"> 
    </label> 
    <label> 
     Day 
     <input type="text" name="day" value="%(day)s"> 
    </label> 
    <label> 
     Year 
     <input type="text" name="year" value="%(year)s"> 
    </label> 
    <div style="color: red">%(error)s</div> 
    <br><br> 
    <input type="submit"> 
</form> 
""" 

class MainPage(webapp2.RequestHandler): 
    def write_form(self, error="", month="", day="", year=""): 
     self.response.out.write(form %{"error": error, 
             "month": escape_html(month), 
             "day": escape_html(day), 
             "year": escape_html(year)}) 

    def get(self): 
     self.write_form() 

    def post(self): 
     user_month = self.request.get('month') 
     user_day = self.request.get('day') 
     user_year = self.request.get('year') 

     month = valid_month(user_month) 
     day = valid_day(user_day) 
     year = valid_year(user_year) 

     if not(month and day and year): 
      self.write_form("That doesn't look valid to me, friend.", user_month, user_day, user_year) 
     else: 
      self.redirect("/thanks") 

class ThanksHandler(webapp2.RequestHandler): 
    def get(self): 
     self.response.out.write("Thanks! That's a totally valid day!") 

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

而代碼GAE和我http://localhost:8081/運行良好,但是失敗了,當我試圖通過在Spyder.the錯誤信息點擊運行該代碼是: 導入錯誤:沒有模塊名爲webapp2的 我已經也看過import webapp2 works on google-app-engine even though I don't have webapp2 installed 並添加GAE目錄到我的〜/ .bashrc老鄉:

export PYTHONPATH="$PYTHONPATH:/home/lehr/Web/google_appengine/" 
export PYTHONPATH="$PYTHONPATH:/home/lehr/Web/google_appengine/lib/" 
export PYTHONPATH="$PYTHONPATH:/home/lehr/Web/google_appengine/lib/yaml" 

但這也不能使用,即使我重新啓動Ubuntu的工作。

回答

1

的GAE應用代碼不旨在直接執行,作爲一個獨立的應用程序,它需要通過它知道如何加載和執行該應用程序代碼(在與仿真GAE蟒沙箱補充它的發展服務器執行功能)。見Using the Local Development Server

可能能夠與SDK來執行它一起(即執行dev_appserver.py並把它傳遞相同ARGS你這麼做的時候,當你得到它沒有Spyder的工作作爲)。但我不熟悉Spyder,我不確定它是否支持通過第三方工具執行您的應用程序代碼(如果它確實 - 如果它真的對開發有實用/有用)

+0

thanks Dan,I只是想知道如果我不使用這個IDE,當我真的需要類似調試和自動完成功能時,我可以使用什麼工具進行開發。 –

+0

我正在使用PyCharm,它同時具有這兩種功能。但是對於GAE應用程序,調試器對於我的口味來說有點太慢,並且認爲它很實用(可能也是因爲運行了SDK),所以大多數時候我都使用基於日誌的調試。付費專業版本爲GAE提供了一些內置的自定義功能,免費社區版本不支持(但我想可以手動設置大部分功能)。 –