为什么我的webapp2导入/ Google App Engine“Hello,World”不起作用?

时间:2012-07-12 00:18:35

标签: python google-app-engine webapp2

虽然我能够在Google App Engine(GAE)上运行我的“Hello,World”程序,但只有在我创建不依赖于webapp2导入的版本时它才有效。为什么导入不起作用?我需要做些什么来解决它?

有效的helloworld.py版本:

print 'Content-Type: text/plain'
print ''
print 'Hello, World!!'

不起作用的helloworld.py版本:

import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, World!')

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

此第二个版本呈现为空白页。

我认为问题是webapp2导入无效。当我从命令行中从我的hello world程序所在的目录中运行python时,我得到以下内容:

Brians-MacBook-Air-2:app_engine_hello_world brian$ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import webapp2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named webapp2

但是,我在以下目录中看到了webapp2.py:

Brians-MacBook-Air-2:webapp2 brian$ pwd
/Users/brian/Repos/app_engine_hello_world/build/webapp2

另外,我正在以下位置安装python 2.7:

Brians-MacBook-Air-2:app_engine_hello_world brian$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python

编辑:添加我的app.yaml文件&amp;其他一些可能有用的信息...

application: hello-world-cs253
version: 1
runtime: python27
api_version: 1
threadsafe: no

handlers:
- url: /.*
  script: helloworld.py

我正在使用SDK / GAE Launcher的版本1.7.0 - 2012-06-26

Chrome,Firefox和Safari中的结果相同

2 个答案:

答案 0 :(得分:4)

您不必安装webapp2。当您使用项目目录的路径作为参数运行dev_appserver.py时,它将处理webapp2的导入。

你有app.yaml,它是否正确?

对于Python2.7,您需要指定您使用的是Python2.7。运行时应该是python27

application: your_app
version: 1
runtime: python27    #important
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: your_app.py

您正在运行什么命令来运行程序?

这样做 -

you@your-computer:~/GAE_folder$ python dev_appserver.py /path/to/your/project/directory

然后在浏览器上打开localhost:8080

编辑 -

我想我知道问题所在。当我第一次尝试在网站上给出的例子时,它也发生在我身上。

将这段代码添加到最后的helloworld.py文件中 -

def main():
    app.run()

if __name__=='__main__':
    main()

答案 1 :(得分:1)

问题出在 app.yaml

  script: helloworld.py

使用python27运行时时,必须将其更改为:

  script: helloworld.app

上面提出的解决方案似乎有效,但严格来说,这是正确的方法。不需要使用main函数。

相关问题