GAE有效,但Spyder导入webapp2失败

时间:2016-10-04 12:27:02

标签: google-app-engine spyder pythonpath

这是在html中测试重定向的简单代码。我使用Spyder编写Python代码。

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中的点击运行代码时失败。错误消息是: ImportError:没有名为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 个答案:

答案 0 :(得分:1)

GAE应用程序代码不是作为独立应用程序直接执行的,它需要由知道如何加载和执行应用程序代码的开发服务器执行(同时使用模拟的GAE python沙箱功能补充它) )。请参阅Using the Local Development Server

可能能够与SDK一起一起执行(即执行dev_appserver.py并传递与你获得它时相同的args没有spyder工作)。但是我不熟悉Spyder,我不确定它是否支持通过第三方工具执行您的应用程序代码(如果确实如此 - 如果它真的对开发很实用/有用)