2010-03-16 58 views
4

我是一名python新手,并开始在Google App Engine上使用Bottle web框架。我一直在搞超级小,超级简单的Hello World示例,并且已经遇到了问题。嘿。我终于得到了代码这个工作......关于导入的新手python错误

import bottle 
from bottle import route 
from google.appengine.ext.webapp import util 

@route('/') 
def index(): 
    return "Hello World!" 

util.run_wsgi_app(bottle.default_app()) 

我的问题是,我想我可能只是去“瓶进口”没有第二行。但是如果我把第二行写出来,我会得到一个NameError。或者如果我'从瓶子导入*',我仍然得到错误。瓶子在我的网站的根目录中只是一个名为'bottle.py'的文件。因此,不管这些工作....

import bottle 
from google.appengine.ext.webapp import util 

@route('/') 
def index(): 
    return "Hello World!" 

util.run_wsgi_app(bottle.default_app()) 

或者

from bottle import * 
from google.appengine.ext.webapp import util 

@route('/') 
def index(): 
    return "Hello World!" 

util.run_wsgi_app(bottle.default_app()) 

该错误消息我得到的是...

Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 3180, in _HandleRequest self._Dispatch(dispatcher, self.rfile, outfile, env_dict) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 3123, in _Dispatch base_env_dict=env_dict) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 515, in Dispatch base_env_dict=base_env_dict) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 2382, in Dispatch self._module_dict) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 2292, in ExecuteCGI reset_modules = exec_script(handler_path, cgi_path, hook) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 2188, in ExecuteOrImportScript exec module_code in script_module.dict File "/Users/tyler/Dropbox/sites/dietgrid/code2.py", line 4, in @route('/') NameError: name 'route' is not defined

所以我错在想它应该能够以其他方式工作还是不行?

回答

8

在你的代码有调用从瓶包装方法的两种不同的方式。

route('/hello') 

bottle.default_app() 

首先调用需要from bottle import routefrom bottle import *和第二个要求import bottle

from foo import bar允许您在代码中使用方法或参数bar,而无需在每次调用时指定包。

3

routebottle模块的一部分。

下应该可以解决问题

import bottle 
... 

@bottle.route('/hello') 
def hello(): 
    return "Hello World!" 

... 
2

您可以只将瓶子导入到命名空间,所以每当您希望从那里使用某些东西时,都会有bottle.作为前缀。

import bottle 
from google.appengine.ext.webapp import util 

@bottle.route('/') 
def index(): 
    return "Hello World!" 

util.run_wsgi_app(bottle.default_app()) 

另一种方法是将您要使用的瓶子的零件导入到命名空间中。

from bottle import route, default_app 
from google.appengine.ext.webapp import util 

@route('/') 
def index(): 
    return "Hello World!" 

util.run_wsgi_app(default_app()) 
5

至于为什么

from bottle import * 

不会做的伎俩:当您导入这样,只有那些在瓶子的_____all_____列表中指定的名称是进口的。所以,如果路径不存在,你必须明确地指定导入:

from bottle import route 
+0

太棒了,谢谢。 – TylerW 2010-03-16 16:37:49

0

我也学会了用一瓶GAE,因为它的尺寸非常小的。你可以做的是直接用你的主文件保存瓶子,这将允许你使用'进口瓶',或放在一个文件夹(从其他文件分开,给一个整洁的结构),并添加一个空的文件__init__.py该文件夹。然后您可以将其导入为import bottle from <foldername>等等。 我写了一篇关于how to use Bottle with GAE的小教程。