关于导入的新手python错误 [英] Newbie python error in regards to import

查看:84
本文介绍了关于导入的新手python错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名python新手,开始在Google App Engine上使用Bottle web框架。我一直在搞超级小,超级简单的Hello World示例,并且已经遇到了问题。嘿。

 导入瓶子
从瓶子导入路线
from google.appengine.ext.webapp import util

@route('/')
def index():
returnHello World!

util.run_wsgi_app(bottle.default_app())

我的问题是,我想我可以在没有第二条线的情况下进入进口瓶。但是如果我把第二行写出来,我会得到一个NameError。或者如果我'从瓶子导入*',我仍然得到错误。瓶子在我的网站的根目录中只是一个名为'bottle.py'的文件。所以这两种工作都没有...

 从google.appengine.ext.webapp导入瓶
导入util

@route('/')
def index():
返回Hello World!

util.run_wsgi_app(bottle.default_app())

p>

 从瓶子导入* 
从google.appengine.ext.webapp导入util

@ route ('/')
def index():
returnHello World!

util.run_wsgi_app(bottle.default_app())

错误消息我得到的是... b
$ b


追溯(最近一次调用最后):

文件
/ 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,
第3123行,在_Dispatch
base_env_dict = env_dict中)文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver。 py,
第515行,调度
base_env_dict = base_env_dict)文件
/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 )在ExecuteCGI
中的文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py,
line 2292 reset_modules = exec_script(handler_path,cgi_path,
hook)文件
/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver .py,
第2188行,ExecuteOrImportScript
exec module_code in script_module。 dict File
/Users/tyler/Dropbox/sites/dietgrid/code2.py ,
第4行,在
@route('/')NameError:name'route'未定义


取值o我错了,认为它应该能够以其他方式工作或者没有?解析方案

  route('/ hello')

  bottle.default_app()

第一次调用需要来自瓶子导入路径从瓶子进口* ,第二个要求进口瓶子

来自foo import bar 可让您在代码中使用方法或参数 bar ,而无需在每次调用时指定包。

I'm a python newbie and starting out with using the Bottle web framework on Google App Engine. I've been messing with the super 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.

这篇关于关于导入的新手python错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆