KeyError on starting flask with Tryton

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.

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 ?

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

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:


Desktop:

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

Are you using a virtualenv?

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

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?

“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

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

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.

A string indicating the name of the database.