[solved] Earliest file to initiate code

Hi.

Which is the file/function that is the earliest initialization available to me to start some code?

I need to start a lib on the top most moment that plone run to be available everywhere on my addon.

Now I'm starting the lib on my-addon/browser/src/__init__.py is this safe?

Just for example, wordpress has an init hook that runs any code that I need immediately after wordpress loads the core.

I tried to find something like that on the documentation without success.

Thanks

Zope/Plone initialization is complex - and influenced via configuration.

Near the end of the initialization, the so called "products" (i.e. packages in the Products namespace) are loaded and site.zcml executed (this pulls in the top level ZCML configuration files). site.zcml and associated configuration folders (such as package-includes) are generated by a buildout run from zcml configuration found in the buildout configuration. In addition z3c.autoinclude may automatically include ZCML configuration for packages. After this, "products" and packages initialization functions (either the initialize function of a "product" or a package's function registered with five:registerPackage) are called. Finally, Zope/Plone starts to handle requests.

Usually, an initialization function is a good place to put in package/product specific initialization. At this time Zope/Plone is not yet guaranteed to be fully initialized but most infrastructure is set up.

You can register for one of the startup events:

https://docs.plone.org/develop/addons/components/events.html#zope-startup-events

Yes, it will be loaded on first import, usually this happens when Zope is loading all the ZCML. If you ensure in you're buildout's instance section zcml = to have your add-on first, it will be imported before all other add-ons.
If you really want to load it before everything else and its worth the effort, you can add to instance section initialization = import myaddon. About limitations here read the documentation plone.recipe.zope2instance · PyPI

I looked in all suggestions and I ended up using the event approach, it worked for everything that I tested so far.

Thanks all.

2 Likes

Plone Foundation Code of Conduct