2012-02-02 53 views
0

我有一個manage.py命令,我使用ftplib來下拉文件。如果我從終端運行此命令,它工作正常。現在我想從腳本中調用它,以便cron可以每晚運行它。當我運行腳本時,它會因「django.db.utils.DatabaseError:no such table」錯誤而失敗。我可以在沒有任何錯誤的情況下從外部腳本運行manage.py,manage.py幫助。有任何想法嗎?Django Manage.py外部腳本錯誤

處理方法從管理命令:

def handle(self, *args, **options): 
     ftp = ftplib.FTP('ftp.somesite.com') 
     ftp.login("anonymous") 
     ftp.cwd('commonupdater') 

     data = [] 
     ftp.dir(data.append) 

     file_lines = [line for line in data if 'mydat' in line and '.zip' in line] 
     files = [line for line in file_lines] 

     for filespec in files: 
      target = filespec.split(' ')[-1] 
      f = open('/tmp/' + target, 'wb') 
      self.getbinary(ftp, target, outfile=f) 
      f.close() 
      self.save_update(target, '/tmp/' + target) 

回溯

local.dbTraceback (most recent call last): 
    File "/home/rb/Workspaces/rms/ravelin/manage.py", line 14, in <module> 
    execute_manager(settings) 
    File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager 
    utility.execute() 
    File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv 
    self.execute(*args, **options.__dict__) 
    File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute 
    output = self.handle(*args, **options) 
    File "/home/rb/Workspaces/rms/ravelin/ravelog/management/commands/fetch_av_updates.py", line 57, in handle 
    self.save_update(target, '/tmp/' + target) 
    File "/home/rb/Workspaces/rms/ravelin/ravelog/management/commands/fetch_av_updates.py", line 30, in save_update 
    if len(av_list) < 1: 
    File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/db/models/query.py", line 82, in __len__ 
    self._result_cache = list(self.iterator()) 
    File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/db/models/query.py", line 273, in iterator 
    for row in compiler.results_iter(): 
    File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 680, in results_iter 
    for rows in self.execute_sql(MULTI): 
    File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql 
    cursor.execute(sql, params) 
    File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/db/backends/util.py", line 34, in execute 
    return self.cursor.execute(sql, params) 
    File "/home/rb/.virtualenvs/ravelin/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 234, in execute 
    return Database.Cursor.execute(self, query, params) 
django.db.utils.DatabaseError: no such table: 
+0

您使用的是哪個數據庫後端?類似這樣的事情可能會發生在sqlite和一個在settings.py中用相對路徑指定的數據庫名稱。 – Raekkeri 2012-02-02 18:45:51

+0

我正在使用sqlite。我現在很確定這是一個路徑/ pythonpath問題。 – RyanBrady 2012-02-02 19:23:44

回答

0

你可以試試這個?找出在命令行中使用的環境變量(set將提供該列表)。把它們放在shell腳本中,最後調用python腳本。在cron中,如下所示調用shell腳本。日誌文件應該包含所有的細節。

 
10 5 * * * /home/myuser/some_wrapper_script.sh > /home/myuser/some_wrapper_script.log 2>&1 
+0

我寫了一個額外的管理命令來檢查os.environ [「PATH」]和sys.path。我檢查了我設置的路徑,一切都很好。我在外部腳本的項目目錄中添加了一個cd,而不是調用manage.py的完整路徑。這似乎解決了它。 – RyanBrady 2012-02-02 20:56:57