开发者

Newbie python error in regards to import

开发者 https://www.devze.com 2022-12-22 21:03 出处:网络
I\'m a python newbie and starting out with using the Bottle web framework on Google App Engine.I\'ve been messing with the sup开发者_如何学Cer small, super easy Hello World sample and have already ran

I'm a python newbie and starting out with using the Bottle web framework on Google App Engine. I've been messing with the sup开发者_如何学Cer small, super easy Hello World sample and have already ran into problems. Heh. I finally got the code to work with this...

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())

My question is, I thought I could just go 'import bottle' without the second line. But if I take the second line out, I get a NameError. Or if I do 'from bottle import *', I still get the error. bottle is just a single file called 'bottle.py' in my site's root directory. So neither of these work....

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

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

util.run_wsgi_app(bottle.default_app())

Or

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

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

util.run_wsgi_app(bottle.default_app())

The error message I get is...

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

So am I wrong in thinking it should be able to work the other ways or no?


In your code you have two different ways of calling methods from bottle package.

route('/hello')

and

bottle.default_app()

First call requires from bottle import route or from bottle import * and second one requires import bottle.

from foo import bar is letting you to use method or parameter bar in your code without specifying the package on each call.


Regarding why

from bottle import *

does not do the trick: when you import like that, only names that are specified in bottle's _____all_____ list are imported. So, if route is not there, you have to specify the import explicitly:

from bottle import route


route is part of the bottle module.

The following should fix the problem

import bottle
...

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

...


You can either just import bottle into you namespace, so every time you wish to use something from there you have bottle. as a prefix.

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

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

util.run_wsgi_app(bottle.default_app())

The other way is to import the parts of bottle you are going to use into your namespace.

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())


I too learned to use Bottle with GAE, because of its very small size. What you can do is keep bottle directly with your main files, that will allow you to use 'import bottle', or put in a folder (to separate it from other files, give a neat structure), and add an empty __init__.py file to that folder. Then you can import it as import bottle from <foldername> and so on. I wrote a small tutorial on how to use Bottle with GAE.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号