michaelliao / awesome-python3-webapp

小白的Python入门教程实战篇
https://liaoxuefeng.com/books/python/
GNU General Public License v3.0
2.4k stars 2.8k forks source link

Date7-mvc 测试出现:NameError: name '__pool' is not defined 求帮忙指点一下 #32

Open Mr1yan opened 7 years ago

Mr1yan commented 7 years ago

找了好久,还是没发现原因,求指点一下 详细代码项目:https://github.com/Mr1yan/awesome-python3-webapp 详细信息: INFO:root:create database connection pool... INFO:root:init jinja2... INFO:root:set jinja2 template path: E:\Python\awesome-python3-webapp\www\templates INFO:root:found model: User (table: users) INFO:root: found mapping: id ==> <StringField, varchar(50):None> INFO:root: found mapping: email ==> <StringField, varchar(50):None> INFO:root: found mapping: passwd ==> <StringField, varchar(50):None> INFO:root: found mapping: admin ==> <BooleanField, boolean:None> INFO:root: found mapping: name ==> <StringField, varchar(50):None> INFO:root: found mapping: image ==> <StringField, varchar(500):None> INFO:root: found mapping: created_at ==> <FloatField, real:None> INFO:root:found model: Blog (table: blogs) INFO:root: found mapping: id ==> <StringField, varchar(50):None> INFO:root: found mapping: user_id ==> <StringField, varchar(50):None> INFO:root: found mapping: user_name ==> <StringField, varchar(50):None> INFO:root: found mapping: user_image ==> <StringField, varchar(500):None> INFO:root: found mapping: name ==> <StringField, varchar(50):None> INFO:root: found mapping: summary ==> <StringField, varchar(200):None> INFO:root: found mapping: content ==> <TextField, text:None> INFO:root: found mapping: created_at ==> <FloatField, real:None> INFO:root:found model: Comment (table: comments) INFO:root: found mapping: id ==> <StringField, varchar(50):None> INFO:root: found mapping: blog_id ==> <StringField, varchar(50):None> INFO:root: found mapping: user_id ==> <StringField, varchar(50):None> INFO:root: found mapping: user_name ==> <StringField, varchar(50):None> INFO:root: found mapping: user_image ==> <StringField, varchar(500):None> INFO:root: found mapping: content ==> <TextField, text:None> INFO:root: found mapping: created_at ==> <FloatField, real:None> INFO:root:add route GET / => index(request) INFO:root:add static /static/ => E:\Python\awesome-python3-webapp\www\static INFO:root:server started at http://127.0.01:9000... INFO:root:Request:GET / INFO:root:Response handler... INFO:root:call with args:{'request': <Request GET / >} INFO:root:SQL: select id, email, passwd, admin, name, image, created_at from users ERROR:aiohttp.server:Error handling request Traceback (most recent call last): File "E:\Python\Python36-32\lib\site-packages\aiohttp\web_protocol.py", line 421, in start resp = yield from self._request_handler(request) File "E:\Python\Python36-32\lib\site-packages\aiohttp\web.py", line 303, in _handle resp = yield from handler(request) File "E:/Python/awesome-python3-webapp/www/app.py", line 46, in logger return (await handler(request)) File "E:/Python/awesome-python3-webapp/www/app.py", line 64, in response r=await handler(request) File "E:\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 113, in handler_wrapper result = yield from result File "E:\Python\awesome-python3-webapp\www\coreweb.py", line 138, in call r=await self._func(**kw) File "E:\Python\Python36-32\lib\asyncio\coroutines.py", line 223, in coro res = yield from await_meth() File "E:\Python\awesome-python3-webapp\www\handlers1.py", line 13, in index users=await User.findAll() File "E:\Python\awesome-python3-webapp\www\orm.py", line 188, in findAll rs = await select(' '.join(sql), args) File "E:\Python\awesome-python3-webapp\www\orm.py", line 32, in select async with pool.get() as conn: NameError: name 'pool' is not defined INFO:aiohttp.access:127.0.0.1 - - [10/Jul/2017:16:47:31 +0000] "GET / HTTP/1.1" 500 330 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"

zaoyilikai commented 7 years ago

The global __pool only needs to be declared in create_pool.

Mr1yan commented 7 years ago

I had made The global __pool only declared in create_pool. you can see my commition。 but still got same error。 please help me !

Mr1yan commented 7 years ago

sorry ! I think i have figure out what‘s wrong in app.py 9 line : from www import orm this way is wrong,cause orm didn't import,I change to:import orm

nothingeasy commented 6 years ago

@Mr1yan @michaelliao what's the difference between from www import orm and import orm ? can anyone explain it for me ? appreciation!

az7852 commented 6 years ago

@nothingeasy from www import orm This orm is the type of: <module 'www.orm' from '/Users/zph/PycharmProjects/test/www/orm.py'> import orm This orm is the type of: <module 'orm' from '/Users/zph/PycharmProjects/test/www/orm.py'>

When you compiled these code in Pycharm, caused by some path problems, it shows error of 'No module named orm'. However it still works. You can right click with this priject and Mark directory as ->Sources Root. With these done, you shall not see these import problems again.

ShawSpring commented 5 years ago

maybe you just forget to call create_pool in a async way.

The global pool declared in create_pool with "sync def", so you should
"await create_pool( pool, ...)" to make sure all other codes running after
pool creation.eg:

async def init(loop):
    await create_pool(loop,user='root',password='xxx',db='xxx')
    u = User(id=66,name='zhaomian' ...)
    await u.save()
    users = await User.findAll()

loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))