的python-NameError:全局名称’create_engine’未定义[尝试创建SQLAlchemyJobStore时]

我正在尝试添加SQLAlchemyJobStore作业存储(并将其设置为默认作业存储)并在其上存储一些作业.我正在运行mysql,其中有一个名为jobstore的数据库.

我有以下程序尝试向正在运行的mysql数据库打开SQLAlchemyJobStore作业存储:

# sqlalchemy.py 
from sqlalchemy import *
from apscheduler.jobstores.sqlalchemy_store import SQLAlchemyJobStore
from apscheduler.scheduler import Scheduler
from datetime import datetime, timedelta
import time

def alarm(time):
    print('Alarm! This alarm was scheduled at %s.' % time)

_aps_config = {'standalone': 'True'}
_dbURL = 'mysql://root:<root-password>@localhost/jobstore'

if __name__ == '__main__':

    scheduler = Scheduler(_aps_config)
    scheduler.add_jobstore(SQLAlchemyJobStore(url=_dbURL), 'default')

    alarm_time = datetime.now() + timedelta(seconds=10)
    scheduler.add_date_job(alarm, alarm_time, name='alarm1', args=[datetime.now()])
    print 'alarms added: ', alarm_time

    alarm_time = datetime.now() + timedelta(seconds=15)
    scheduler.add_date_job(alarm, alarm_time, name='alarm2', args=[datetime.now()])
    print 'alarms added: ', alarm_time

    alarm_time = datetime.now() + timedelta(seconds=20)
    scheduler.add_date_job(alarm, alarm_time, name='alarm3', args=[datetime.now()])
    print 'alarms added: ', alarm_time

    try:
        scheduler.start()
    except (KeyboardInterrupt, SystemExit):
        scheduler.shutdown()
        pass

尝试运行上述代码时,我看到以下内容:

NameError: global name 'create_engine' is not defined

$python sqlalchemy.py
Traceback (most recent call last):
  File "sqlalchemy.py", line 19, in <module>
    scheduler.add_jobstore(SQLAlchemyJobStore(url=_dbURL), 'default')
  File "/usr/lib/python2.7/site-packages/APScheduler-2.1.0-py2.7.egg/apscheduler/jobstores/sqlalchemy_store.py", line 29, in __init__
    self.engine = create_engine(url)
NameError: global name 'create_engine' is not defined
$

我看到“ /usr/lib/python2.7/site-packages/APScheduler-2.1.0-py2.7.egg/apscheduler/jobstores/sqlalchemy_store.py”,__init__正在尝试create_engine及其失败.

 20 class SQLAlchemyJobStore(JobStore):
 21     def __init__(self, url=None, engine=None, tablename='apscheduler_jobs',
 22                  metadata=None, pickle_protocol=pickle.HIGHEST_PROTOCOL):
 23         self.jobs = []
 24         self.pickle_protocol = pickle_protocol
 25 
 26         if engine:
 27             self.engine = engine
 28         elif url:
 29             self.engine = create_engine(url)

这是怎么了?!换句话说,如何使用APScheduler创建SQLAlchemyJobStore并成功在其上存储作业?任何示例/代码片段都会有很大的帮助!

解决方法:

您的代码似乎没有问题.我尝试运行它,并成功完成以下输出:

python jobstore.py
alarms added:  2013-02-07 10:31:10.234000
alarms added:  2013-02-07 10:31:15.240000
alarms added:  2013-02-07 10:31:20.240000

我所做的唯一更改是更新_dbURL =’sqlite:///:memory:’以使用sqlite引擎.

请检查您是否安装了sqlalchemy,并且可以通过脚本在PYTHONPATH中找到它.
在python控制台中运行以下代码,或者最好将其添加到脚本的开头,然后检查输出.

import sqlalchemy
print sqlalchemy.__version__ 

更新
我重新阅读了您的文章,并意识到我的测试代码还有另一个区别-我创建了另一个名称为jobstore.py的文件

我试图将文件重命名为sqlalchemy.py并得到相同的异常:

Traceback (most recent call last):
  File "C:/*/so/sqlalchemy.py", line 22, in <module>
    scheduler.add_jobstore(SQLAlchemyJobStore(url=_dbURL), 'default')
  File "C:\Progs\Python27\lib\site-packages\apscheduler\jobstores\sqlalchemy_store.py", line 29, in __init__
    self.engine = create_engine(url)
NameError: global name 'create_engine' is not defined

Process finished with exit code 1

基本上,问题在于您的python脚本名称与sqlalchemy模块名称具有相同的名称,因此python首先加载您的脚本,并且无法访问sqlalchemy代码.

尝试将脚本名称重命名为sqlalchemy.py以外的名称-如果您安装了sqlalchemy模块,这将有所帮助.

上一篇:【DB笔试面试695】在Oracle中,什么是Oracle RDA(Remote Diagnostic Agent)工具?


下一篇:Python使用APScheduler实现定时任务