Odoo Help

2

How to get a Python shell with the Odoo environment?

By
Kasper Souren
on 6/11/14, 10:46 AM 8,863 views

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()

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.

Hannes Smit
on 6/17/14, 3:30 AM
2
Bernardo Kyotoku
On 2/10/17, 1:16 PM

In version 10: ./odoo-bin shell -d <database>

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

2
Marvin Taboada
On 6/20/14, 1:41 AM

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.

0
Nehemias Herrera
On 1/7/18, 2:00 PM

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




0

Jérémy Kersten (jke)

--Jérémy Kersten (jke)--

4720
| 3 3 5
Jodoigne, Belgium
--Jérémy Kersten (jke)--

Jérémy is a member of the core R&D team since september 2013. He developed several projects for OpenERP version 8 including the eCommerce, the Google Calendar synchronization and the new product variants/configurator. Now, he manages Odoo website and themes for saas.

Jérémy Kersten (jke)
On 3/16/15, 5:58 PM

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']

0
john doe
On 2/10/17, 9:28 AM

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

0
Manuel A. Márquez
On 10/4/16, 5:43 PM

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

-1
Wouter Tebbens
On 6/19/14, 8:03 AM

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

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

3 follower(s)

Stats

Asked: 6/11/14, 10:46 AM
Seen: 8863 times
Last updated: 1/7/18, 2:00 PM