2
votes

I created an Odoo Module in Python using the Python library ujson. I installed this library on my development server manually with pip install ujson.

Now I want to install the Module on my live server. Can I somehow tell the Odoo Module to install the ujson library when it is installed? So I just have to add the Module to my addons path and install it via the Odoo Web Interface?

Another reason to have this automated would be if I like to share my custom module, so others don't have to install the library manually on their server.

Any suggestions how to configure my Module that way? Or should I just include the library's directory in my module?

4

4 Answers

4
votes

Thank you for your help, @Walid Mashal and @CZoellner, you both pointed me to the right direction.

I solved this task now with the following code added to the __init__.py of my module:

import pip
try:
    import ujson
except ImportError:
    print('\n There was no such module named -ujson- installed')
    print('xxxxxxxxxxxxxxxx installing ujson xxxxxxxxxxxxxx')
    pip.main(['install', 'ujson'])
3
votes

You should try-except the import to handle problems on odoo server start:

try:
    from external_dependency import ClassA
except ImportError:
    pass

And for other users of your module, extend the external_dependencies in your module manifest (v9 and less: __openerp__.py; v10+: __manifest__.py), which will prompt a warning on installation:

"external_dependencies": {
    'python': ['external_dependency']
},

Big thanks goes to Ivan and his Blog

1
votes

In python file using the following command, you can install it (it works for odoo only). Eg: Here I am going to install xlsxwriter

try:
    import xlsxwriter
except:
    os.system("pip install xlsxwriter")
    import xlsxwriter
0
votes

The following is the code that is used in odoo base module report in base addons inside report.py (odoo_root_folder/addons/report/models/report.py) to install wkhtmltopdf.

from openerp.tools.misc import find_in_path
import subprocess

def _get_wkhtmltopdf_bin():
    return find_in_path('wkhtmltopdf')

try:
    process = subprocess.Popen([_get_wkhtmltopdf_bin(), '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except (OSError, IOError):
    _logger.info('You need Wkhtmltopdf to print a pdf version of the reports.')

basically you need to find some python code that will run the library and install it and include that code in one of you .py files, and that should do it.