# KeyError on starting flask with Tryton

**URL:** <https://discuss.tryton.org/t/keyerror-on-starting-flask-with-tryton/3187>\
**Category:** Developer\
**Tags:** flask\
**Created:** [September 4, 2020, 6:28pm UTC](https://discuss.tryton.org/t/keyerror-on-starting-flask-with-tryton/3187 "2020-09-04T18:28:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yoeldiomedez](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/yoeldiomedez/32/943_2.png) [@yoeldiomedez](https://discuss.tryton.org/u/yoeldiomedez)\
**Post date:** [September 4, 2020, 6:28pm UTC](https://discuss.tryton.org/t/keyerror-on-starting-flask-with-tryton/3187/1 "2020-09-04T18:28:55Z")

</div>

Hello,  
I’m trying to run the example but I get the following `KeyError`:

```
Traceback (most recent call last):
  File "/home/yoel/Development/tryton/marketing/app/app.py", line 11, in <module>
    User = tryton.pool.get('res.user')
  File "/home/yoel/Development/tryton/marketing/venv/lib/python3.6/site-packages/trytond/pool.py", line 179, in get
    return self._pool[self.database_name][type][name]
KeyError: 'res.user'

```

This is how looks my `app.py` file:

```
from flask import Flask
from flask_tryton import Tryton
app = Flask( __name__ )

app.config['TRYTON_CONFIG'] = '/home/yoel/Development/tryton/5.0/full/conf/trytond.conf'
app.config['TRYTON_DATABASE'] = 'tryton_full_new'
app.config['TRYTON_USER'] = 1

tryton = Tryton(app, configure_jinja=True)
User = tryton.pool.get('res.user')

@tryton.default_context
def default_context():
    return User.get_preferences(context_only=True)

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

@app.route('/user/<record("res.user"):user>')
@tryton.transaction()
def user(user):
    return user.name

@app.route('/users/<records("res.user"):users>')
@tryton.transaction()
def users(users):
    return ', '.join(u.name for u in users)

if __name__ == ' __main__':
    app.run(debug=True, port=5000)

```

Could anyone tell me how can I make it work? Thanks.

---

<div class="post-metadata">

**Author:** ![German](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/german/32/143_2.png) [@German](https://discuss.tryton.org/u/German)\
**Post date:** [September 4, 2020, 6:39pm UTC](https://discuss.tryton.org/t/keyerror-on-starting-flask-with-tryton/3187/2 "2020-09-04T18:39:00Z")

</div>

You need a working trytond server first. Flask\_tryton it’s another interface for your existing trytond server (Like tryton client ). Did you check trytond.conf? Are you able to connect using tryton client on same server ?

---

<div class="post-metadata">

**Author:** ![iehoshia](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/iehoshia/32/243_2.png) [@iehoshia](https://discuss.tryton.org/u/iehoshia)\
**Post date:** [September 4, 2020, 6:43pm UTC](https://discuss.tryton.org/t/keyerror-on-starting-flask-with-tryton/3187/3 "2020-09-04T18:43:51Z")

</div>

Hi,

Make sure your TRYTON\_CONFIG and TRYTON\_DATABASE variables are well configured.

AFAIK you don’t need to setup TRYTON\_USER (default is used, 0):

I’m sure you will find more information here

> **[Client Challenge](https://pypi.org/project/flask-tryton/)**

---

<div class="post-metadata">

**Author:** ![yoeldiomedez](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/yoeldiomedez/32/943_2.png) [@yoeldiomedez](https://discuss.tryton.org/u/yoeldiomedez)\
**Post date:** [September 4, 2020, 7:57pm UTC](https://discuss.tryton.org/t/keyerror-on-starting-flask-with-tryton/3187/4 "2020-09-04T19:57:20Z")

</div>

My trytond.conf it’s fine and working, I’m able to connect by web and desktop client  
`trytond.conf`:

```
[database]
uri = postgresql://tryton_full_new:tryton_full_new@127.0.0.1:5432
path = /home/yoel/Development/tryton/5.0/full/attach
list = True
retry = 5
language = es_419

[session]
timeout = 7200

[web]
listen = 127.0.0.1:8005
root = /home/yoel/Development/tryton/5.0/full/sao

[email]
uri = smtp://localhost:25
from = Tryton ERP Perú <noreply@connecttix.pe> 

[account_invoice]
sunat_invoicing = /home/yoel/Development/tryton/5.0/full/sunat/invoices
debug = development

```

Web:

 ![image](https://discuss-cdn.tryton.org/uploads/default/original/2X/d/ddd75936cb22f1d20b5c75803066c17d65204610.png)  
Desktop:  
 ![image](https://discuss-cdn.tryton.org/uploads/default/original/2X/4/4aa9aaf84a1be89f6ea0902bc39d8203f6ebc85c.png)

---

<div class="post-metadata">

**Author:** ![yoeldiomedez](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/yoeldiomedez/32/943_2.png) [@yoeldiomedez](https://discuss.tryton.org/u/yoeldiomedez)\
**Post date:** [September 4, 2020, 8:08pm UTC](https://discuss.tryton.org/t/keyerror-on-starting-flask-with-tryton/3187/5 "2020-09-04T20:08:55Z")

</div>

I come from there, looking for some help here. About the variables I edited my post and I replied to @German

---

<div class="post-metadata">

**Author:** ![iehoshia](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/iehoshia/32/243_2.png) [@iehoshia](https://discuss.tryton.org/u/iehoshia)\
**Post date:** [September 4, 2020, 8:16pm UTC](https://discuss.tryton.org/t/keyerror-on-starting-flask-with-tryton/3187/6 "2020-09-04T20:16:58Z")

</div>

Are you using a virtualenv?

If yes, You need to active tryton\_flask inside the same virtualenv that trytond is running.

---

<div class="post-metadata">

**Author:** ![yoeldiomedez](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/yoeldiomedez/32/943_2.png) [@yoeldiomedez](https://discuss.tryton.org/u/yoeldiomedez)\
**Post date:** [September 15, 2020, 4:13pm UTC](https://discuss.tryton.org/t/keyerror-on-starting-flask-with-tryton/3187/7 "2020-09-15T16:13:52Z")

</div>

Yes, I’m using virtualenv, I’ve moved the Flask script to the same virtualenv that uses Tryton, but I’m still getting the same KeyError that I posted at the beginning of this topic.

Is it imperative use the same virtualenv for Tryton and Flask?

---

<div class="post-metadata">

**Author:** ![yoeldiomedez](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/yoeldiomedez/32/943_2.png) [@yoeldiomedez](https://discuss.tryton.org/u/yoeldiomedez)\
**Post date:** [September 15, 2020, 4:30pm UTC](https://discuss.tryton.org/t/keyerror-on-starting-flask-with-tryton/3187/8 "2020-09-15T16:30:51Z")

</div>

“There are three configuration options available” says the package:

```
>>> from flask import Flask
>>> from flask_tryton import Tryton
>>> app = Flask( __name__ )
>>> app.config['TRYTON_DATABASE'] = 'tryton_full_new'
>>> tryton = Tryton(app, configure_jinja=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/yoel/Development/tryton/new/full/env/lib/python3.6/site-packages/flask_tryton.py", line 52, in __init__
    self.init_app(app)
  File "/home/yoel/Development/tryton/new/full/env/lib/python3.6/site-packages/flask_tryton.py", line 67, in init_app
    with Transaction().start(database, user, readonly=True):
  File "/home/yoel/Development/tryton/new/full/env/lib/python3.6/site-packages/trytond/transaction.py", line 107, in start
    database = backend.Database(database_name).connect()
  File "/home/yoel/Development/tryton/new/full/env/lib/python3.6/site-packages/trytond/backend/sqlite/database.py", line 344, in connect
    raise IOError('Database "%s" doesn\'t exist!' % path)
OSError: Database "/home/yoel/db/tryton_full_new.sqlite" doesn't exist!

```

Why is looking for a .sqlite database? I mean, it’s not clear if the TRYTON\_DATABASE is just a string or an URI

---

<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:** [September 15, 2020, 4:44pm UTC](https://discuss.tryton.org/t/keyerror-on-starting-flask-with-tryton/3187/9 "2020-09-15T16:44:27Z")

</div>

> [@yoeldiomedez](#):
>
> Is it imperative use the same virtualenv for Tryton and Flask?

Yes, you should run the same code so flask is able to load the Tryton modules.

> [@yoeldiomedez](#):
>
> Why is looking for a .sqlite database?

Because you did not set up any trytond.conf file, so it’s using the default one which uses sqlite.  
You should set the TRYTOND\_CONFIG environment variable pointing to the route of your configuration file to use a diferent backend.

> [@yoeldiomedez](#):
>
> I mean, it’s not clear if the TRYTON\_DATABASE is just a string or an URI

A string indicating the name of the database.
