Skip to content

Instantly share code, notes, and snippets.

@dreispt
Created November 19, 2014 11:04
Show Gist options
  • Save dreispt/d14e424540faedb3ea88 to your computer and use it in GitHub Desktop.
Save dreispt/d14e424540faedb3ea88 to your computer and use it in GitHub Desktop.
Odoo Shell: run Odoo commands without a server RPC connection
"""
Setup:
Assuming Odoo 8.0 sources at ~/odoo:
$ cp odoo-sh.py ~/odoo
$ cd ~/odoo
$ python -i odoo-sh.py
Usage example:
>>> env = connect('my-db-name')
>>> Users = env['res.users']
>>> Users.search()
>>> Users.browse(1).name
u'Administrator'
"""
from __future__ import print_function
from openerp.modules.registry import RegistryManager
from openerp.api import Environment
def connect(dbname='trunk', uid=1, context=None):
r = RegistryManager.get(dbname)
cr = r.cursor()
Environment.reset()
env = Environment(cr, uid, context or {})
print('Connected to %s with user %s %s'
% (dbname, env.uid, env.user.name))
return env
if __name__ == '__main__':
print(__doc__)
@lepistone
Copy link

Brilliant! This works for core addons, but he doesn't find models from custom modules. I think it's because the full startup injects addons-path into the python path, but we don't do that here.

Do you know a way around that?

@antonylesuisse
Copy link

Could you refactor is as cli subcommand ?

./odoo.py shell -d dbname

./odoo.py --addons-path=xxxx shell -d dbname

would directly work. I will merge it if done as subcommand.

example of such commands are in openerp/cli/deploy.py

@dreispt
Copy link
Author

dreispt commented Nov 20, 2014

@antonylesuisse It would be great to have this directly from odoo.py. I'll give a try at that.

@dreispt
Copy link
Author

dreispt commented Nov 20, 2014

@lepistone I had that doubt, because odoo.pydoes have a loop where it loads all the addons. I'll have a look at that too.

@hardik-emipro
Copy link

@dreispt
I tried keeping the module in core addons path.
I ran ./odoo.py shell -d my_db_name , on terminal, but it didn't jump to the python console and returns to normal command prompt.

@fadeldamen
Copy link

I same problem hardik-empiprotechnologies i am install module shell in database down server and start with ./odoo.py shell -d my_db_name --addons-path="dir/module/shel" and noting console returns to bash

@jeffery9
Copy link

jeffery9 commented Oct 1, 2016

how to run shell in jupyter notebook , any idea ?

@nordiccoding
Copy link

hi jeffery, late answer anyhow I was searching for same ... just add !
!./odoo/odoo-bin shell -c odoo_oca.conf --log-level=error

@YYMPhoo
Copy link

YYMPhoo commented Mar 12, 2019

Creating record from this terminal cannot be seen from ui(actually not created but it return id)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment