0

I want to add some python codes after syncdb, so I decide to write a build.py which does everything including syncdb.

I write something in build.py as:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.management import call_command
from django.contrib.auth.models import User
call_command('syncdb', interactive=False)

But when I run build.py, it said:

Traceback (most recent call last):
  File "/home/csimstu/PycharmProjects/TeenHope/TeenHope/build.py", line 5, in <module>
    call_command('syncdb', interactive=False)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 161, in call_command
    return klass.execute(*args, **defaults)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 255, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 385, in handle
    return self.handle_noargs(**options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py", line 56, in handle_noargs
    cursor = connection.cursor()
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 324, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 306, in _cursor
    self._sqlite_create_connection()
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 296, in _sqlite_create_connection
    self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file

I've tried ./manage.py syncdb and use call_command in interactive shell mode, and both ways worked perfectly okay. How could it be?

Mike Lee
  • 1,859
  • 2
  • 14
  • 15

1 Answers1

1

If the database file isn't found that's probably because you didn't use a full path in your settings.

import os
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.realpath(os.path.join(os.path.dirname(__file__), "relative_path_here", "database.db")),

Out of topic: I'd recommend to use Fabric for this kind of commands.

François Constant
  • 5,531
  • 1
  • 33
  • 39
  • I've said that it works fine using syncdb. So I don't think it's the problem of full path. – Mike Lee Aug 02 '13 at 03:21
  • OK but did you try? You've got nothing to lose making it absolute ! Another one, did you try to change the rights on that file and parent directory? – François Constant Aug 02 '13 at 03:25
  • Sorry buddies, it's my own fault. I defined something in settings.py like `PROJECT_PATH = os.getcwd()`. But when running `build.py`, `PROJECT_PATH` is set to the current working directory, not the path of the project. By changing that to what you suggested, it works fine now. Thanks. – Mike Lee Aug 02 '13 at 03:36
  • NW, yes make sense, that's what getcwd does :) – François Constant Aug 02 '13 at 03:41