2010-03-16 30 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的小教程。