12

I'm running ubuntu 9.04 32b and got django from Synaptics. My settings.py is configured for a sqlite3 database.

I've been through this tutorial and got the following error when trying to run the command python manage.py syncdb :

 Traceback (most recent call last): File "manage.py", line 11, in execute_manager(settings) File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 340, in execute_manager utility.execute() File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 295, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 192, in run_from_argv self.execute(*args, **options.__dict__) File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 219, in execute output = self.handle(*args, **options) File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 348, in handle return self.handle_noargs(**options) File "/usr/local/lib/python2.6/dist-packages/django/core/management/commands/syncdb.py", line 51, in handle_noargs cursor = connection.cursor() File "/usr/local/lib/python2.6/dist-packages/django/db/backends/__init__.py", line 56, in cursor cursor = self._cursor(settings) File "/usr/local/lib/python2.6/dist-packages/django/db/backends/sqlite3/base.py", line 145, in _cursor self.connection = Database.connect(**kwargs) sqlite3.OperationalError: unable to open database file 

Do anyone have a clue on my problem ?

9 Answers 9

21

In settings.py are you using a relative path to the sqlite file?

If you are, try changing that to an absolute path.

i.e. instead of:

~/project/mydata.db 

use

/home/user/project/mydata.db 
Sign up to request clarification or add additional context in comments.

1 Comment

This is almost always the problem. Python doesn't do path expansion when it reads these.
17

This can also happen if your database name is the same as your project name. To fix it just change the name of your db e.g. by adding a .db to the NAME. So if your DATABASE NAME in settings.py was 'epic_project' change it to 'epic_project.db'


Long and boring explanation:

If you start your project by running:

django startproject epic_project 

your folder structure will be like this:

  • /path/to/epic_project/
    • manage.py
    • epic_project/
      • settings.py

if then in your settings.py you set your database as:

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'epic_project', ... } } 

when

python manage.py syncdb 

runs it tries to open or create a sqlite db file at /path/to/epic_project/epic_project, but there is a directory there, so it says "oh ok the path exists already, let's open it as an sqlite db", unfortunately for sqlite it's a directory and not a DB, so it cries and django presents these tears to you as "sqlite3.OperationalError: unable to open database file"

1 Comment

plus 1 for the humour
9

might be a permission problem, does your user have sufficient right to write on the folder? for example if you do

sudo python manage.py syncdb 

instead, does it work?

Comments

5

For Google's sake:

The path to the database must be the full path to the file --- not just the directory where the file lives.

1 Comment

Isn't this what JosefAssad suggested in their answer?
3

I had the same problem on Windows then realized that syncdb would not create the specified folder if it didn't already exist. I had specified c:/mysite/db/sqlite3.db in settings but the /db/ folder didn't exist. Created it in terminal then re-ran syncdb successfully.

Comments

2

I had the same problem two days ago. I solved it by setting the 'NAME' in the DATABASE dictionary (settings.py) to the absolute path. For example.

settings.py

DATABASES = { 'default' : { 'ENGINE' : 'django.db.backends.sqlite3', 'NAME' : DATABASE_PATH, } } 

here you can set the DATABASE_PATH at the top of the settings.py as such (Not sure if this will work for windows)

SETTINGS_DIR = os.path.dirname(__file__) PROJECT_PATH = os.path.abspath(os.path.join(SETTINGS_DIR, os.pardir)) DATABASE_PATH = os.path.abspath(os.path.join(PROJECT_PATH, 'your-database-name')) 

For Windows you might have to use the replace method. (Not sure .. But you can try it out as follows)

PROJECT_PATH = os.path.abspath(os.path.join(SETTINGS_DIR, os.pardir).replace('\\', '/')) 

Same goes for the DATABASE_PATH. PS. Correct me if I am wrong.

Comments

0

Similar to answer from user104264 - different perspective...

Currently following the YouTube tutorial I had the same error. Seems to me while running the manage.py in a virtualenv django project directory ...path to virtual env.../django_project/, the database name inside /myapp/settings.py was simply 'storage.db' so

(django-v)...path to virtual env project.../django_project/>python manage.py syncdb

created ...path to virtual env project.../django_project/storage.db

-Bill

Comments

0

If you are specifying a full path to db file and are still having issues, try putting an r before the string.

ie.

r'C:\home\usr\mysite\sqlite3.db' 

3 Comments

That signifies a regular expression, but the problem you're solving is the fact Python doesn't like the single backslashes. You'd be better off with 'C:\\home\\usr\\mysite\\sqlite3.db'
@Tom it does not signify a regular expression. It tells python to treat it as a raw string literal: stackoverflow.com/a/2081708/2259303
Duh, my bad. 99% of the time I use it is for Django urls so I've got it stuck in my head that way. Sorry.
0

The problem I was running into was a bootstrapping issue where some model lookups were being done at import (outside of any class or function). Per the docs, this is a bad idea.

Comments