Newbie Python Error In Regards To Import
Solution 1:
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.
Solution 2:
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
Solution 3:
route
is part of the bottle
module.
The following should fix the problem
import bottle
...
@bottle.route('/hello')defhello():
return"Hello World!"
...
Solution 4:
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('/')defindex():
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('/')defindex():
return"Hello World!"
util.run_wsgi_app(default_app())
Solution 5:
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.
Post a Comment for "Newbie Python Error In Regards To Import"