Odoo Environment#

class odoo_tools.api.environment.Environment(context=None)[source]#

Odoo Environment object.

The odoo environment object is a container that store all the information required to prepare an environment for odoo.

It can be used to browse the available modules in the configured odoo environment.

Or to setup environment variables for the running odoo instance.

It can also be used to configure the odoo.cfg file based on the current environment.

context#

The context to use

Type:

Context

env#

The environment variables container

Type:

Environment

requirement_file_path#

The path where to store the requirements file when merging pip requirements.

Type:

Path

addons_paths()[source]#

Returns the addons path configured for this environment.

For example, it would find the path addons in the odoo installed folder. And in custom paths defined to search for addons.

If an addons path had modules in /a/b, /a/d/e/f and /a/b/c. It would return the following list of addons paths.

['/a/b', '/a/b/c', '/a/d/e/f']

Odoo doesn’t load folders recursively so if you have modules within folder that also contains modules. The addons_paths have to be defined.

With this, you can store your modules in to '/addons' and only define as custom_paths '/addons'. If there are modules in '/addons/**'. Those will be returned by addons_paths().

addons_paths = ",".join(env.addons_paths())

with env.config():
    env.set_config("addons_path", addons_paths)
Returns:

The list of paths containing installable

addons.

Return type:

paths (List<Path>)

check_odoo()[source]#
config(readonly=False)[source]#

A context manager that can be used to read/write configuration for odoo.

The context manager saves the configuration file when it is closed.

with env.config():
    env.set_config('server_wide_modules', 'web,base')
env_options()[source]#

Load environment variable options.

get_config(key, section='options')[source]#

Get a configuration settings in the currently open configuration file.

odoo_config()[source]#

Returns odoo config regardless of being in openerp/odoo

odoo_options()[source]#
odoo_version()[source]#

Returns the odoo version.

In case odoo isn’t installed, it will fallback to the context variable odoo_version which can be set through environment variable ODOO_VERSION.

Returns:

The major version number or None

Return type:

int|None

package_map()[source]#

Returns a package map.

The package map is used to map some module name to python package names.

By default, in newer versions of odoo, it will check for the package name. But unfortunately some modules will still have the module name defined in their python external dependencies.

When requirements are built from the odoo modules available in the odoo environment. It will wrongly attempt to install let say the module “ldap”. ldap is the name of the module that can be imported, but the package that needs to be installed is python-ldap.

Such map could look like this:

{'ldap': 'python-ldap'}

Then when the packages required are found, they can be mapped to those package names to find the exact python package name.

Mapping a name to an empty string would remove the name from the requirements. This can be useful to remove package defined in the external dependencies that aren’t actual packages but builtin dependencies that would be already part of python itself.

{'asyncio': ''}

The behaviour could be also useful when you want to install an alternative to let say the barcode module.

Returns:

Key, Value of mapped module/package name.

Return type:

dict

path()[source]#

Returns the path where odoo is installed and can be imported.

The base addons are installed in the folder addons relative to this path.

requirement_files(lookup_requirements=False)[source]#
set_config(key, value, section='options')[source]#

Set a configuration settings in the currently open configuration file.

Example:

env.set_config('server_wide_modules', 'web,base')
env.set_config('max_handlers', '3', 'custom_section')
Parameters:
  • key (str) – The name of the config to set.

  • value (str) – The value to store in the config

  • section (str) – The section to use in the ConfigParser object. By default, it uses the 'options' section. This is the default section used by odoo. But the section can be set to something different to define additional sections for use.

sync_options()[source]#

Sync options to odoo configmanager

Loads the config file and loads the environment variables options. Then set the options into odoo.tools.config in the options and misc parameters.