Help

4

How to get a Python shell with the Odoo environment?

Avatar
Kasper Souren

How to get a Python shell with the Odoo environment?

Ideally IPython.

I don't have a clear idea what I want to achieve but over the years I've come to love the possibility of Python (and JavaScript, as opposed to PHP) to just be in a shell with all the relevant code and figure out things. Much better way to learn and code than reading a book or even API documentation.

I started playing around a bit.

    $ ipython

    import openerp

    openerp.cli.main()

1 Comment
Avatar
Discard
Avatar
Hannes Smit
-

Kasper, may I ask what you want to achieve? `python openerp-server` should do start the server. Have a look at the help for flags / configurations.

8 Answers
2
Avatar
Bernardo Kyotoku
Best Answer

In version 10: ./odoo-bin shell -d <database>  --addons-path='your/addons/path'

http://stackoverflow.com/questions/34293139/how-to-run-odoo-orm-methods-in-the-python-console

Avatar
Discard
2
Avatar
Marvin Taboada
Best Answer

Hello Kasper,

It seems you're using a source distribution where all addons have been moved out of the "openerp.addons" Python package to the root directory of the distribution (current layout in GitHub, branch 7.0), hence it cannot load some required modules. Specifying "--addons-path/addons_path" is required in order to fix this.

You can fake command line arguments in the Python interactive interpreter in the following way:

>>> import sys
>>> import openerp
>>>
>>> sys.argv = ['', '--addons-path=/opt/openerp/server/addons', '--log-level=debug']
>>>
>>> openerp.cli.main()

Please note that the first argument is an empty string.

Regards.

Avatar
Discard
0
Avatar
Wouter Tebbens
Best Answer

Hey Kasper, have a look at this thread, it's precisely about IDE's that people use for OpenERP v7 / odoo development:

https://www.odoo.com/forum/Help-1/question/Tools--IDE-for-OpenERP7-source-and-report-development-5536

Avatar
Discard
0
Avatar
Muhammad Awais
Best Answer

 You can also use...           

  • $ python3 odoo-bin shell -d db_name

Avatar
Discard
0
Avatar
Nehemias Herrera
Best Answer

Hi, I am trying to get to the interactive shell with odoo 11 but I am getting the error

odoo@21ad17f724ce:/$ odoo shell -d universidad

2018-01-07 18:53:39,951 134 INFO ? odoo: Odoo version 11.0-20171030

2018-01-07 18:53:39,952 134 INFO ? odoo: Using configuration file at /etc/odoo/odoo.conf

2018-01-07 18:53:39,952 134 INFO ? odoo: addons paths: ['/var/lib/odoo/addons/11.0', '/mnt/extra-addons', '/usr/lib/python3/dist-packages/odoo/addons']

2018-01-07 18:53:39,953 134 INFO ? odoo: database: default@default:default

2018-01-07 18:53:39,963 134 INFO ? odoo.service.server: Initiating shutdown

2018-01-07 18:53:39,963 134 INFO ? odoo.service.server: Hit CTRL-C again or send a second signal to force the shutdown.

2018-01-07 18:53:39,964 134 INFO ? odoo.sql_db: Connection to the database failed

Traceback (most recent call last):

File "/usr/lib/python3/dist-packages/odoo/modules/registry.py", line 59, in __new__

return cls.registries[db_name]

File "/usr/lib/python3/dist-packages/odoo/tools/func.py", line 68, in wrapper

return func(self, *args, **kwargs)

File "/usr/lib/python3/dist-packages/odoo/tools/lru.py", line 44, in __getitem__

a = self.d[obj].me

KeyError: 'universidad'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/usr/bin/odoo", line 8, in <module>

odoo.cli.main()

File "/usr/lib/python3/dist-packages/odoo/cli/command.py", line 60, in main

o.run(args)

File "/usr/lib/python3/dist-packages/odoo/cli/shell.py", line 124, in run

self.shell(config['db_name'])

File "/usr/lib/python3/dist-packages/odoo/cli/shell.py", line 110, in shell

registry = odoo.registry(dbname)

File "/usr/lib/python3/dist-packages/odoo/__init__.py", line 76, in registry

return modules.registry.Registry(database_name)

File "/usr/lib/python3/dist-packages/odoo/modules/registry.py", line 61, in __new__

return cls.new(db_name)

File "/usr/lib/python3/dist-packages/odoo/modules/registry.py", line 73, in new

registry.init(db_name)

File "/usr/lib/python3/dist-packages/odoo/modules/registry.py", line 141, in init

with closing(self.cursor()) as cr:

File "/usr/lib/python3/dist-packages/odoo/modules/registry.py", line 472, in cursor

return self._db.cursor()

File "/usr/lib/python3/dist-packages/odoo/sql_db.py", line 634, in cursor

return Cursor(self.__pool, self.dbname, self.dsn, serialized=serialized)

File "/usr/lib/python3/dist-packages/odoo/sql_db.py", line 178, in __init__

self._cnx = pool.borrow(dsn)

File "/usr/lib/python3/dist-packages/odoo/sql_db.py", line 517, in _locked

return fun(self, *args, **kwargs)

File "/usr/lib/python3/dist-packages/odoo/sql_db.py", line 585, in borrow

**connection_info)

File "/usr/lib/python3/dist-packages/psycopg2/__init__.py", line 164, in connect

conn = _connect(dsn, connection_factory=connection_factory, async=async)

psycopg2.OperationalError: could not connect to server: No such file or directory

Is the server running locally and accepting

connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?


I tried from the official odoo docker image and also with bitnami odoo image, same error, I am able to get into it without the -d argument but this way there's no environment, how can I either start the interactive shell properly or how to load the environment if I got into the interactive shell whitout specifying at database.

there's another error

odoo@21ad17f724ce:/$ odoo shell

2018-01-07 18:58:41,137 149 INFO ? odoo: Odoo version 11.0-20171030

2018-01-07 18:58:41,137 149 INFO ? odoo: Using configuration file at /etc/odoo/odoo.conf

2018-01-07 18:58:41,137 149 INFO ? odoo: addons paths: ['/var/lib/odoo/addons/11.0', '/mnt/extra-addons', '/usr/lib/python3/dist-packages/odoo/addons']

2018-01-07 18:58:41,137 149 INFO ? odoo: database: default@default:default

2018-01-07 18:58:41,148 149 INFO ? odoo.service.server: Initiating shutdown

2018-01-07 18:58:41,148 149 INFO ? odoo.service.server: Hit CTRL-C again or send a second signal to force the shutdown.

No environment set, use `/usr/bin/odoo shell -d dbname` to get one.

odoo: <module 'odoo' from '/usr/lib/python3/dist-packages/odoo/__init__.py'>

openerp: <module 'odoo' from '/usr/lib/python3/dist-packages/odoo/__init__.py'>

Python 3.5.3 (default, Jan 19 2017, 14:11:04)

[GCC 6.3.0 20170118] on linux

Type "help", "copyright", "credits" or "license" for more information.

(Console)

>>> dir()

['__builtins__', 'odoo', 'openerp']

>>> from odoo.service import db

>>> db.list_dbs()

2018-01-07 18:59:15,171 149 INFO ? odoo.sql_db: Connection to the database failed

Traceback (most recent call last):

File "<console>", line 1, in <module>

File "/usr/lib/python3/dist-packages/odoo/service/db.py", line 362, in list_dbs

with closing(db.cursor()) as cr:

File "/usr/lib/python3/dist-packages/odoo/sql_db.py", line 634, in cursor

return Cursor(self.__pool, self.dbname, self.dsn, serialized=serialized)

File "/usr/lib/python3/dist-packages/odoo/sql_db.py", line 178, in __init__

self._cnx = pool.borrow(dsn)

File "/usr/lib/python3/dist-packages/odoo/sql_db.py", line 517, in _locked

return fun(self, *args, **kwargs)

File "/usr/lib/python3/dist-packages/odoo/sql_db.py", line 585, in borrow

**connection_info)

File "/usr/lib/python3/dist-packages/psycopg2/__init__.py", line 164, in connect

conn = _connect(dsn, connection_factory=connection_factory, async=async)

psycopg2.OperationalError: could not connect to server: No such file or directory

Is the server running locally and accepting

connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

>>>

Any help will be appreciated :) thanks




Avatar
Discard
0
Best Answer

Not compatible with V7, because it's new in saas-6...

But that can help other peoples looking about it...

./odoo.py shell -d saas6

after, all is possible ...

OLD API : 

 >>> blog_post_ids = env.registry['blog.post'].search(env.cr, env.uid, [], context=env.context)
>>> env.registry['blog.post'].browse(env.cr, env.uid, blog_post_ids, context=env.context).mapped('name')

[u'The Future of Emails', u'Integrating your CMS and E-Commerce']


NEW API:

>>> env['blog.post'].search([]).mapped('name')

[u'Integrating your CMS and E-Commerce', u'The Future of Emails']

Avatar
Discard
0
Avatar
john doe
Best Answer

Same situation, shell module is installed. When i type sudo ./odoo.py shell -d v8dev it says:

"ERROR ? openerp.sql_db: Connection to the database failedTraceback (most recent call last): OperationalError: IMPORTANR  "root" role is not exist"

Fix: type just ./odoo.py shell -d v8dev

Avatar
Discard
0
Avatar
Manuel A. Márquez
Best Answer

I know it's too late but you can use erppeek. https://erppeek.readthedocs.io/en/latest/

Avatar
Discard