web2py db未定义

Hen*_*ock 2 python web2py

我正在尝试在命令行运行脚本,该脚本使用带有以下命令的模型:

c:\web2py>python web2py.py -M -N -S automate -R applications/automate/modules/eventserver.py
Run Code Online (Sandbox Code Playgroud)

但我一直收到错误:

web2py Web Framework
Created by Massimo Di Pierro, Copyright 2007-2011
Version 1.99.7 (2012-03-04 22:12:08) stable
Database drivers available: SQLite3, pymysql, pg8000, IMAP
Traceback (most recent call last):
  File "c:\web2py\gluon\shell.py", line 206, in run
    execfile(startfile, _env)
  File "applications/automate/modules/eventserver.py", line 6, in <module>
    deviceHandler = devicehandler.DeviceHandler()
  File "applications\automate\modules\devicehandler.py", line 10, in __init__
    self.devices = self.getActiveDevices()
 File "applications\automate\modules\devicehandler.py", line 18, in getActiveDe
vices
    print db
NameError: global name 'db' is not defined
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

编辑:根据我的研究,我只找到了解决方案"将-M添加到你的命令",但我已经完成了它,但它仍然无法工作.

edit2:我的db.py中有db = DAL('sqlite://storage.sqlite')所以应该加载

Ant*_*ony 8

edit2:我的db.py中有db = DAL('sqlite://storage.sqlite')所以应该加载

假设db.py位于/ models文件夹中,那么db在此处创建的对象将在以后执行的模型文件以及控制器和视图中可用,但在导入的模块中将不可用.相反,您必须将db对象传递给模块中的函数或类.另一个选项是将db对象添加到current线程本地对象,然后可以在模块中导入和访问该对象:

在/models/db.py中:

from gluon import current
db = DAL('sqlite://storage.sqlite')
current.db = db
Run Code Online (Sandbox Code Playgroud)

在/modules/eventserver.py中:

from gluon import current
def somefunction():
    db = current.db
    [do something with db]
Run Code Online (Sandbox Code Playgroud)

注意,如果确实db在模块中定义了对象,请不要在顶层定义它 - 在函数或类中定义它.

有关更多详细信息,请参阅有关模块和current的书籍部分.