Hello world!
How do you structure your Odoo projects in PyCharm? After years of programming Odoo using PyCharm, I want to share with you how I do it. I find it working great so far, not only for me but also for my colleagues.
For example, I have the following Odoo related folders:
- odoo15, odoo16: corresponding to the 15.0, 16.0 branches of the Odoo community repository (https://github.com/odoo/odoo).
- odoo-stubs15, odoo-stubs16: corresponding to the 15.0, 16.0 branches of the Odoo stubs repository (https://github.com/odoo-ide/odoo-stubs).
- account-invoicing15, account-invoicing16: corresponding to the 15.0, 16.0 branches of the OCA invoicing addons repository (https://github.com/OCA/account-invoicing).
- stock-logistics-warehouse15, stock-logistics-warehouse16: corresponding to the 15.0, 16.0 branches of the OCA warehouse addons repository (https://github.com/OCA/stock-logistics-warehouse).
Basically, there are two ways to structure these folders in PyCharm: (1) using content roots or (2) using project dependencies. Both need to meet the following requirements:
- Works with multiple Odoo versions and multiple custom addons repositories at the same time without switching between projects and windows.
- Works well with the Odoo plugin.
Use content roots
This is my preferred structure. The above folders are grouped by Odoo version. One project per Odoo version. In one project, I will add all related folders as the content roots of the project. Here is the step by step:
- Open the odoo15 folder as the main project.
- In the same PyCharm window, open the odoo16 folder and choose Attach.
- Open Settings > Project > Python Interpreter to setup Python interpreters for odoo15 and odoo16 projects.
- Open Settings > Project > Project Structure. Select the odoo15 project, click Add Content Root to add the following folders: odoo-stubs15, account-invoicing15, stock-logistics-warehouse15. Do the same for the odoo16 project.
The result will look like the following screenshot:
Use Project Dependencies
This structure is flatter. Each custom addons branch is a separate project. It all depends on another project - the odoo source code. Here is the step by step:
- Open the odoo15 folder as the main project.
- In the same PyCharm window, open the account-invoicing15 folder and choose Attach. Do the same for other folders except odoo-stubs*. Once done, we will have the following projects: odoo15, account-invoicing15, stock-logistics-warehouse15, odoo16, account-invoicing16, stock-logistics-warehouse16.
- Open Settings > Project > Python Interpreter to setup Python interpreters for these projects.
- Open Settings > Project > Project Dependencies. Select the account-invoicing15 project and set the dependency is the odoo15 project. Do the same for other custom addons projects.
- Open Settings > Project > Project Structure. Select the odoo15 project, click Add Content Root to add the odoo-stubs15 folder. Do the same for the odoo16 project.
The result will look like the following screenshot:
Custom scopes
To navigate code more efficiently in a multiple Odoo versions workspace like above, you can create custom scopes. I always have several custom scopes, one for each Odoo version. This way I can use these scopes to search for files, symbols,... more efficiently. For example:
Searching without scope will yield confusing results:
Searching with a specific scope will yield only relevant results:
After this article, I hope you can use PyCharm and the Odoo plugin more efficiently.