4 - Managing Dependencies and External Python Libraries
As your students progress in their Odoo 19 journey, they will quickly realize that they don't have to reinvent the wheel. Often, they will need to build upon existing Odoo modules (like Sales or Accounting) or use external Python libraries (like pandas for data analysis or requests for API calls).
Odoo handles both of these scenarios gracefully through the __manifest__.py file.
1. Internal Dependencies (Odoo Modules)
If your custom module relies on existing Odoo objects, views, or logic, you must declare that relationship. This guarantees Odoo installs the required modules before attempting to install yours.
This is done using the depends list in the __manifest__.py file:
{
'name': 'Custom Sales Reporting',
'version': '19.0.1.0.0',
# This tells Odoo: "Do not install me unless 'sale' and 'account' are already installed."
'depends': ['sale', 'account'],
'data': [
'views/custom_sale_views.xml',
],
}
2. External Dependencies (Python Libraries & Binaries)
When your students need a third-party Python package that is not part of the standard Python library, they must explicitly declare it. Odoo 19 uses the external_dependencies dictionary to check if the host server has the required tools installed.
If the system is missing the library, Odoo will block the installation of the module and show a clear error to the user, preventing a server crash.
{
'name': 'Currency API Integrator',
'version': '19.0.1.0.0',
'depends': ['base'],
# Declare external Python packages and system binaries here
'external_dependencies': {
'python': ['requests', 'pandas'], # Checks for pip packages
'bin': ['wkhtmltopdf'] # Checks for system-level tools
},
}
3. Safe Importing in Python (try / except)
Even though the __manifest__.py blocks installation, Odoo's framework still scans all Python files in the addons path during server startup. If you use a standard import pandas and the server doesn't have it, the entire Odoo service might fail to start.
💡 PRO TIP: Teach your students to always wrap external library imports in a try-except block inside their __init__.py or .py models.
import logging
from odoo import models, fields
_logger = logging.getLogger(__name__)
# Safely import the external library
try:
import pandas as pd
except ImportError:
_logger.warning("The 'pandas' library is not installed. Please install it using pip.")
pd = None
class CustomDataProcessor(models.Model):
_name = 'custom.data.processor'
_description = 'Data Processor'
def process_data(self):
if not pd:
return False
# Continue using pandas safely...
4. Environment Management (The requirements.txt File)
In modern operating systems like Ubuntu 24.04 (which Odoo 19 targets), installing Python packages globally using pip install is restricted (due to PEP 668).
Your students must use a Virtual Environment (venv). To make deployment easy for other developers, they should include a requirements.txt file at the root of their custom module repository:
# requirements.txt
requests==2.31.0
pandas>=2.0.0
When a new developer downloads the module, they simply activate their Odoo virtual environment and run:
pip install -r requirements.txt
There are no comments for now.