1

I am doing a refactor of our code base and was suggested we create a python module that will store functions that are used across different modules in Odoo 14 (sale, project for example), this is in a different file in the structure. My question is, Odoo will allow importing the new relative module and that way call the function I need?

Trying to import the module gives me ModulenotFoundError: no module named ...

from rw_utilities import working_hours_calculation 

With the research that I've done, this should be possible with a simple python app but inside Odoo for the moment I'm not too clear. We run the application in the Odoo.sh cloud Not local.

File structure the goal is modules like sales and project can use the same function.

enter image description here

Ron
  • 91
  • 8
  • working_hours_calculation is not int rw_utilities. it is in rw_utilities.models – Farid Fakhry Jul 28 '22 at 17:51
  • Thank you, but still it gives me the error "NoModuleNameError" – Ron Jul 28 '22 at 17:55
  • what is your working directory when you launch the script? – Farid Fakhry Jul 28 '22 at 18:00
  • can you paste the full eror so we know what module is not found – Farid Fakhry Jul 28 '22 at 20:02
  • File "/home/odoo/src/user/rw_salescustomize/models/sale_order_line.py", line 15, in from rw_utilities.models import working_hours_calculation ModuleNotFoundError: No module named 'rw_utilities' this is like the main error log, – Ron Jul 28 '22 at 20:10
  • and where is your console/terminal when you run the script – Farid Fakhry Jul 28 '22 at 20:11
  • I get what you mean I don't run a script because my environment is up in Odoo.sh, I'm triggering an action in the UI that in theory should call the function in the new module but for the moment I doesn't recognize this as a module and throws the error in the console. – Ron Jul 28 '22 at 20:32
  • try this before importing ```py import sys import os sys.path.append(os.path.dirname(os.path.dirname(__file__)))``` – Farid Fakhry Jul 28 '22 at 20:40

1 Answers1

2

If you log something like that.

import logging
_logger = logging.getLogger(__name__)
_logger.info("Test")

Then you should get a log line like that, and it contains the right path to the module:

2022-01-01 00:01:01,111 1 INFO hostname odoo.addons.your_module.models.your_file: Test

And it should be usable like that

from odoo.addons.your_module.models.your_file import your_item

Another way would be:


class MyTools(models.Model):
    _inherit = 'my.tools'

    @api.model
    def _my_function(self):
        pass

# to call
self.env['my.tools']._my_function()

# or even
my_function = self.env['my.tools']._my_function
my_function()

Paxmees
  • 1,540
  • 1
  • 15
  • 28