0

I have a manage.py command I wrote that uses ftplib to pull down a file. If I run this command from the terminal it works fine. Now I want to call it from a script so that cron can run it nightly. When I run the script, it fails with "django.db.utils.DatabaseError: no such table" error. I can run manage.py, manage.py help from the external script without any errors. Any ideas?

handle method from manage command:

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)

Traceback

    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:
Tunaki
  • 132,869
  • 46
  • 340
  • 423
RyanBrady
  • 6,633
  • 4
  • 27
  • 32
  • Which database backend are you using? Something like this might happen with sqlite and a database name specified with relative path in settings.py. – Raekkeri Feb 02 '12 at 18:45
  • I'm using sqlite. I'm now pretty sure it was a path/pythonpath issue. – RyanBrady Feb 02 '12 at 19:23

1 Answers1

0

Can you try this ? Find out the environment variables that are being used at the command line (set will provide that list). Put these in a shell script and at the very end call the python script. In cron, call the shell script as shown below. The log file should contain all the details.

10 5 * * * /home/myuser/some_wrapper_script.sh > /home/myuser/some_wrapper_script.log 2>&1
souser
  • 5,868
  • 5
  • 35
  • 50
  • I wrote an additional management command to check os.environ["PATH"] and sys.path. I checked that against the paths I had set and everything looked fine. I added a cd to the project directory in the external script instead of calling the full path to manage.py. This seemed to fix it. – RyanBrady Feb 02 '12 at 20:56