2013-02-07 68 views
1

我正在嘗試添加SQLAlchemyJobStore作業存儲(並將其作爲default作業存儲)並在其上存儲一些作業。我正在運行mysql,它有一個名爲jobstore的數據庫。NameError:未定義全局名稱'create_engine'[嘗試創建SQLAlchemyJobStore時]

我有試圖打開一個SQLAlchemyJobStore工作店裏的mysql分貝下面的程序運行:

# 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併成功將作業存儲在它們上?任何示例/代碼片段將是一個很好的幫助!

回答

2

在你的代碼中似乎沒有問題。我試圖運行它,它與下面的輸出成功完成:

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__ 

UPDATE 我重讀您的文章,並意識到,我的測試代碼有一個更差 - 我創建了另一名的文件:jobstore.py

我試圖重命名文件sqlalchemy.py和得到了同樣的異常:

Traceback (most recent call last): 
    File "C:/stackoverflow/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的模塊名稱而不能訪問的SQLAlchemy碼。

嘗試將腳本名稱重命名爲sqlalchemy.py - 如果您安裝了sqlalchemy模塊,這將有所幫助。

+1

你是對的;在APScheduler谷歌小組上討論過同樣的問題,一位來自社區的紳士也強調了這一點!謝謝! +1 –

+0

謝謝你節省了一天 –

相關問題