# sqlite3.OperationalError: unable to open database file

**URL:** https://discuss.tryton.org/t/sqlite3-operationalerror-unable-to-open-database-file/8114
**Category:** Developer
**Tags:** flask
**Created:** [December 12, 2024, 1:08pm UTC](https://discuss.tryton.org/t/sqlite3-operationalerror-unable-to-open-database-file/8114 "2024-12-12T13:08:09Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![landras1](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/l/779978/32.png) [@landras1](https://discuss.tryton.org/u/landras1)
#### Post date: [December 12, 2024, 1:08pm UTC](https://discuss.tryton.org/t/sqlite3-operationalerror-unable-to-open-database-file/8114/1 "2024-12-12T13:08:09Z")

</div>

Hello Guys!

I have a very simple API using flask\_tryton. It was developed under Tryton 5.4, and we migrated to Tryton 6.0 on the production and testing servers, where it works perfectly.

However, on my local development environment, I am encountering an error and cannot start it.

```auto
Traceback (most recent call last):
  File "/home/lims/src/api_flask/ __init__.py", line 70, in <module>
    tryton = Tryton(app)
  File "/home/lims/src/venv_test/lib/python3.7/site-packages/flask_tryton.py", line 51, in __init__
    self.init_app(app)
  File "/home/lims/src/venv_test/lib/python3.7/site-packages/flask_tryton.py", line 66, in init_app
    with Transaction().start(database, user, readonly=True):
  File "/home/lims/src/venv_test/lib/python3.7/site-packages/trytond/transaction.py", line 107, in start
    database = backend.Database(database_name).connect()
  File "/home/lims/src/venv_test/lib/python3.7/site-packages/trytond/backend/sqlite/database.py", line 355, in connect
    factory=SQLiteConnection)
sqlite3.OperationalError: unable to open database file

```

For the migration of my local development environment, a virtual environment with the same name as the old one was used to avoid changing any files.

I made sure that the Tryton configuration files `/home/lims/src/trytond.conf` and the `config.cfg` file used by flask\_tryton are being read (by temporarily renaming the files, which triggered a specific error).

I’m very confused

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [December 12, 2024, 2:04pm UTC](https://discuss.tryton.org/t/sqlite3-operationalerror-unable-to-open-database-file/8114/2 "2024-12-12T14:04:30Z")

</div>

The error is raised because you are connecting to a Sqlite databse which does not exist.

This may be due to some issues:

- If you have your database on postgresql, the server is not reading the proper config file, so it uses the default values (sqlite)
- If your database is on postgres it may be created on the wrong path or does not exist. Make sure that a file named `database.sqlite` (where database is the name of yourdatabase) exists under the directory pointed by the `[database]` `path` value of your configuration. If you do not have any configuration setting, the database will be read from the `db` folderd in the home directory of the user running the server
- Also make sure that the user running trytond has read permissions on the config files otherwise the file will be ignored.

In order to debug the problem, I will recommend running `trytond` with verbose flag as the server will print the configuration file used in such mode.

Hope this helps!

---

<div class="post-metadata">

### Author: ![landras1](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/l/779978/32.png) [@landras1](https://discuss.tryton.org/u/landras1)
#### Post date: [December 12, 2024, 2:20pm UTC](https://discuss.tryton.org/t/sqlite3-operationalerror-unable-to-open-database-file/8114/3 "2024-12-12T14:20:12Z")

</div>

Hola Sergi

Se supone que está todo bien, yo corro tryton con un alias en .bash\_rc que es este comando:  
trytond -d lantos\_testing -c ~/src/trytond.conf

lims@debian:~$ pwd  
/home/lims

y no tienen ningún problema. Renombré temporalmente los archivos para verificar que eran los correctos

En el trytond.conf tengo estas primeras líneas, y se conecta con postgres sin problema:

[cache]  
model = 300

[database]  
uri = postgresql://lims:passlims@/

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [December 12, 2024, 3:02pm UTC](https://discuss.tryton.org/t/sqlite3-operationalerror-unable-to-open-database-file/8114/4 "2024-12-12T15:02:14Z")

</div>

¿Puedes copiarnos el comando que ejecturas cuando te da error? No me queda claro cuando te da el error.

---

<div class="post-metadata">

### Author: ![landras1](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/l/779978/32.png) [@landras1](https://discuss.tryton.org/u/landras1)
#### Post date: [December 12, 2024, 3:17pm UTC](https://discuss.tryton.org/t/sqlite3-operationalerror-unable-to-open-database-file/8114/5 "2024-12-12T15:17:12Z")

</div>

El error es al ejecutar el archivo que tiene el API , **init**.py con el comando usual:

lims@debian:~/src/api\_flask$ /home/lims/src/venv\_test/bin/python /home/lims/src/api\_flask/ **init**.py

Esto funcionaba antes de pasar a Tryton 6.0 ()y en los serves de producción y testing funcionan, pero corren con gunicorn, en desarrollo local uso el servidorde desarrollo de flask. El contenido del\_\_init\_\_.py comienza acá:

# -_- coding: utf8 -_-

import os  
from functools import wraps  
from datetime import datetime, timedelta  
from typing import List  
from itertools import chain

from pydantic import BaseModel, ValidationError, validator

from trytond.model.modelstorage import RequiredValidationError  
from trytond.transaction import Transaction  
from flask import Flask, jsonify, request  
from flask\_cors import CORS  
from flask\_tryton import Tryton

def create\_app(config=None):  
‘’‘Create Flask APP’‘’  
app = Flask( **name** )  
app.config.from\_pyfile(config)

```
return app

```

path = os.path.dirname(os.path.realpath( **file** ))  
print(path)  
conf\_file = ‘%s/config.cfg’ % path

app = create\_app(conf\_file)  
app.config[‘JSON\_AS\_ASCII’] = False  
app.config[‘JSON\_SORT\_KEYS’] = False

# Update: For Flask 2.3 and later use this:

# app.json.sort\_keys = False

CORS(app)

…

# algunas funciones y clases acá

…

# y acá salta el error

tryton = Tryton(app)

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [December 12, 2024, 4:55pm UTC](https://discuss.tryton.org/t/sqlite3-operationalerror-unable-to-open-database-file/8114/6 "2024-12-12T16:55:35Z")

</div>

Seguramente el problema es que no tienes la variable de entorno `TRYTOND_CONFIG` apuntando a tu fichero de configuración. Al no encontrar esta variable entonces coje los valores por defecto y por eso te esta intentando ejecutar el SQLite

---

<div class="post-metadata">

### Author: ![landras1](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/l/779978/32.png) [@landras1](https://discuss.tryton.org/u/landras1)
#### Post date: [December 12, 2024, 5:30pm UTC](https://discuss.tryton.org/t/sqlite3-operationalerror-unable-to-open-database-file/8114/7 "2024-12-12T17:30:01Z")

</div>

Tengo esto igual que en producción:

lims@debian:~/src/api\_flask$ cat config.cfg  
TRYTON\_DATABASE = ‘lantos\_testing’  
TRYTON\_USER = 1  
TRYTON\_CONFIG = ‘/home/lims/src/trytond.conf’  
lims@debian:~/src/api\_flask$ ls /home/lims/src/trytond.conf  
/home/lims/src/trytond.conf

ahora renombro el .conf para verificar que intentó buscarlo:

lims@debian:~/src/api\_flask$ ls /home/lims/src/trytond.conf\*  
/home/lims/src/trytond.conf.renombrado

y ejecuto:  
lims@debian:~/src/api\_flask$ /home/lims/src/venv\_test/bin/python /home/lims/src/api\_flask/\_\_init\_\_.py

y sale el mensaje, que me confirma que sí lo intenta leer,pero obviamente lo renombré:  
could not load /home/lims/src/trytond.conf

---

<div class="post-metadata">

### Author: ![landras1](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/l/779978/32.png) [@landras1](https://discuss.tryton.org/u/landras1)
#### Post date: [December 13, 2024, 2:07pm UTC](https://discuss.tryton.org/t/sqlite3-operationalerror-unable-to-open-database-file/8114/8 "2024-12-13T14:07:50Z")

</div>

Hola Sergi

Es como decías tuve que hacer en línea de comandos:  
export TRYTOND\_CONFIG=/home/lims/src/trytond.conf

y lo agregué al .bashrc.

Me confundió que no era necesario antes de hacer la migración de 5.4 a 6.0, y en los servidores de producción y testing no veo dónde está seteada esa variable (yo no hago la administración de los servidores).

Muchas gracias!

---

<div class="post-metadata">

### Author: ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)
#### Post date: [December 14, 2024, 2:08pm UTC](https://discuss.tryton.org/t/sqlite3-operationalerror-unable-to-open-database-file/8114/9 "2024-12-14T14:08:09Z")

</div>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
