Kasper Souren Kasper Souren - 6 months ago 35
Python Question

How can I import an Odoo/OpenERP Addon module in an interactive python environment?

How do I import an Odoo/OpenERP addon module from a python shell?

I want to learn more about the structure of Odoo. I prefer to do that through IPython, but I'm not sure how to import addons into the environment. For starts I merely want to load a default Addon into my environment. So I just copied a line from the default Product module. I did not modify anything in the source code. I have been grepping through the source code to find out why I can't simply import the Addon in the I'm used to with Python.

My Odoo installation works fine.

$ cd /opt/odoo

$ ipython

In [1]: import openerp

In [2]: openerp.modules.module?

[not much luck]

In [3]: openerp.addons?

[not much luck either, nothing here either]

In [4]: import openerp.addons

[no error]

In [5]: import openerp.addons.decimal_precision as dp # Line from addons/product/product.py

[....]

ImportError: No module named decimal_precision


openerp.addons
doesn't have anything but still
import openerp.addons.STUFF
works fine from Odoo addon modules.

I have the feeling that
addons
needs to be initialized but I haven't found out how to do that. I started going through the code from
openerp.main.cli()
.

openerp.tools.config.parse_config()
is a step in the right direction but it's not enough. I need to somehow pass
--addons-path=addons
as well (since Odoo is not smart enough to find its own addons).

Answer

My advice is for you to use the Odoo shell command:

$ ./odoo.py shell -d <your_db>

You will get an interactive shell with self available as if you were inside a model method, although in plain Python rather than IPython. It is available in 9.0, and has a community backport for 8.0.

Another alternative is to use ERPPeek: it is a Python client, connecting to a remote Odoo server, that can run interactive commands similar to (but not the same as) the ones used in the server. An advantage of this is that you don't need Odoo installed on your side, and can choose to run ERPPeek with the Python version you prefer.

Comments