# OSError: Database does not exist when running Flask API with Tryton

**URL:** https://discuss.tryton.org/t/oserror-database-does-not-exist-when-running-flask-api-with-tryton/8283
**Category:** Developer
**Created:** [February 7, 2025, 9:20am UTC](https://discuss.tryton.org/t/oserror-database-does-not-exist-when-running-flask-api-with-tryton/8283 "2025-02-07T09:20:06Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![mahija](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mahija](https://discuss.tryton.org/u/mahija)
#### Post date: [February 7, 2025, 9:20am UTC](https://discuss.tryton.org/t/oserror-database-does-not-exist-when-running-flask-api-with-tryton/8283/1 "2025-02-07T09:20:06Z")

</div>

I am trying to run a Flask API using Tryton, but I am encountering the following error when starting the application:

```auto
OSError: Database '/home/username/db/database.sqlite' doesn't exist!

```

I have set the `TRYTON_DATABASE` configuration to `'database'`, but it seems like the application cannot find the database file at the specified location (`/home/username/db/database.sqlite`).

Here is the relevant part of my Flask app:

```auto
from flask import Flask, request, jsonify
from trytond.pool import Pool
from trytond.config import config
from trytond.transaction import Transaction
from flask_tryton import Tryton

app = Flask( __name__ )
app.config['TRYTON_DATABASE'] = 'database'
tryton = Tryton(app, configure_jinja=True)

config.update_etc('/home/username/project/backoffice/tryton/trytond.conf')  
Pool.start()

@app.before_request
def log_request():
    print(f"=== Request masuk: {request.method} {request.path} ===")

@app.route('/')
@tryton.transaction()
def hello():
     user, = User.search([('login', '=', 'admin')])
     return '%s, Hello World!' % user.name

```

I have verified that the database file doesn’t exist at `/home/username/db/database.sqlite`. What are the recommended steps to either create the database or ensure that the application connects to the correct database?

Additionally, if the database file needs to be created, what would be the correct procedure for setting it up with Tryton?

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [February 7, 2025, 10:13am UTC](https://discuss.tryton.org/t/oserror-database-does-not-exist-when-running-flask-api-with-tryton/8283/2 "2025-02-07T10:13:17Z")

</div>

The `trytond` configuration must be loaded before flask\_tryton is intialized.  
The simplest way is to set the environment `TRYTON_CONFIG`.

---

<div class="post-metadata">

### Author: ![mahija](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mahija](https://discuss.tryton.org/u/mahija)
#### Post date: [February 12, 2025, 6:18am UTC](https://discuss.tryton.org/t/oserror-database-does-not-exist-when-running-flask-api-with-tryton/8283/3 "2025-02-12T06:18:58Z")

</div>

Sorry, I didn’t quite understand. Do you mean that I need to run `trytond -c trytond.conf` first?

---

<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: [February 12, 2025, 8:24am UTC](https://discuss.tryton.org/t/oserror-database-does-not-exist-when-running-flask-api-with-tryton/8283/4 "2025-02-12T08:24:36Z")

</div>

You need to set the environment variable `TRYOND_CONFIG` with the path of your configuration file. This way the flask server will read the settings from this file and connect to your postgres database. Something like:

`$ TRYTOND_CONFIG=/home/username/project/backoffice/tryton/trytond.conf flask run `

Otherwise the flask server uses the default database settings and tries to connect to a sqlite database.

---

<div class="post-metadata">

### Author: ![mahija](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mahija](https://discuss.tryton.org/u/mahija)
#### Post date: [February 12, 2025, 12:06pm UTC](https://discuss.tryton.org/t/oserror-database-does-not-exist-when-running-flask-api-with-tryton/8283/5 "2025-02-12T12:06:33Z")

</div>

```auto
app = Flask( __name__ )

app.config['TRYTOND_CONFIG'] = '/home/username/tryton/trytond.conf'

app.config['TRYTON_DATABASE'] = 'database'

tryton = Tryton(app, configure_jinja=True)

```

set the TRYTOND\_CONFIG environment variable as instructed, but I’m still encountering the same error.

Is there anything else I should check or configure to ensure Flask is correctly reading the Tryton settings?

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [February 12, 2025, 12:34pm UTC](https://discuss.tryton.org/t/oserror-database-does-not-exist-when-running-flask-api-with-tryton/8283/6 "2025-02-12T12:34:10Z")

</div>

> [@mahija](#):
>
> set the TRYTOND\_CONFIG environment variable as instructed

It is `TRYTON_CONFIG` that I instructed.

---

<div class="post-metadata">

### Author: ![mahija](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mahija](https://discuss.tryton.org/u/mahija)
#### Post date: [February 12, 2025, 1:09pm UTC](https://discuss.tryton.org/t/oserror-database-does-not-exist-when-running-flask-api-with-tryton/8283/7 "2025-02-12T13:09:56Z")

</div>

```auto
from flask import Flask, request, jsonify
from trytond.pool import Pool
from trytond.config import config
from trytond.transaction import Transaction
from trytond.protocols.wrappers import with_pool, with_transaction
import traceback
from flask_tryton import Tryton

app = Flask( __name__ )
app.config['TRYTON_CONFIG'] = '/home/username/tryton/trytond.conf'
app.config['TRYTON_DATABASE'] = 'database'
tryton = Tryton(app, configure_jinja=True)

# debugger
@app.before_request
def log_request():
    print(f"=== Request masuk: {request.method} {request.path} ===")

@app.route('/')
@tryton.transaction()
def hello():
     user, = User.search([('login', '=', 'admin')])
     return '%s, Hello World!' % user.name 

@app.route("/api/hello", methods=["GET"])
def hello():
    print("=== API /api/hello dipanggil! ===") # Jika ini tidak muncul, berarti request tidak sampai ke sini
    return jsonify({"message": "Hello from Flask!"})

@app.route("/database/login", methods=['POST'])
@with_transaction()
def login():

```

I did this but it still get this error

```auto
(env) user@pop-os:~/tryton/modules/api$ python api_tryton.py
Traceback (most recent call last):
  File "/home/username/tryton/modules/api/api_tryton.py", line 16, in <module>
    tryton = Tryton(app, configure_jinja=True)
  File "/home/username/.virtualenvs/env/lib/python3.10/site-packages/flask_tryton.py", line 52, in __init__
    self.init_app(app)
  File "/home/username/.virtualenvs/env/lib/python3.10/site-packages/flask_tryton.py", line 67, in init_app
    with Transaction().start(database, user, readonly=True):
  File "/home/username/tryton/trytond/trytond/transaction.py", line 107, in start
    database = backend.Database(database_name).connect()
  File "/home/username/tryton/trytond/trytond/backend/sqlite/database.py", line 353, in connect
    self._make_uri(), uri=True,
  File "/home/username/tryton/trytond/trytond/backend/sqlite/database.py", line 425, in _make_uri
    raise IOError("Database '%s' doesn't exist!" % db_path)
OSError: Database '/home/username/db/database.sqlite' doesn't exist!

```

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [February 12, 2025, 1:23pm UTC](https://discuss.tryton.org/t/oserror-database-does-not-exist-when-running-flask-api-with-tryton/8283/8 "2025-02-12T13:23:04Z")

</div>

You must not import `trytond` stuff before having flask\_tryton initialized.

---

<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: [February 13, 2025, 1:23pm UTC](https://discuss.tryton.org/t/oserror-database-does-not-exist-when-running-flask-api-with-tryton/8283/9 "2025-02-13T13:23:55Z")

</div>

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