Zope.event throws an ImportError

Hi. When starting a Plone 5.1.5 instance I get an ImportError in Zope2.Startup.init.py from "from zope.event import notify". I put a pdb just before the import call and below you can see it works the first time through but not the second:

/data/plone-buildout/profiles/cache/eggs/Zope2-2.13.27-py2.7.egg/Zope2/Startup/init.py(28)()
-> from zope.event import notify
(Pdb) w
/data/plone-buildout/bin/instance-debug(292)()
-> import plone.recipe.zope2instance.ctl
/data/plone-buildout/profiles/cache/eggs/plone.recipe.zope2instance-4.4.0-py2.7.egg/plone/recipe/zope2instance/ctl.py(36)()
-> from Zope2.Startup import zopectl
/data/plone-buildout/profiles/cache/eggs/Zope2-2.13.27-py2.7.egg/Zope2/init.py(60)()
-> from Zope2.Startup.run import configure
/data/plone-buildout/profiles/cache/eggs/Zope2-2.13.27-py2.7.egg/Zope2/Startup/init.py(28)()
-> from zope.event import notify
(Pdb) import zope.event
(Pdb) c
/data/plone-buildout/profiles/cache/eggs/Zope2-2.13.27-py2.7.egg/Zope2/Startup/init.py(28)()
-> from zope.event import notify
(Pdb) w
/data/plone-buildout/parts/instance-debug/bin/interpreter(314)()
-> exec(compile(__file__f.read(), file, "exec"))
/data/plone-buildout/profiles/cache/eggs/Zope2-2.13.27-py2.7.egg/Zope2/Startup/run.py(76)()
-> run()
/data/plone-buildout/profiles/cache/eggs/Zope2-2.13.27-py2.7.egg/Zope2/Startup/run.py(17)run()
-> import Zope2.Startup
/data/plone-buildout/profiles/cache/eggs/Zope2-2.13.27-py2.7.egg/Zope2/init.py(60)()
-> from Zope2.Startup.run import configure
/data/plone-buildout/profiles/cache/eggs/Zope2-2.13.27-py2.7.egg/Zope2/Startup/init.py(28)()
-> from zope.event import notify
(Pdb) import zope.event
*** ImportError: No module named event
(Pdb)

Mike Metcalfe via Plone Community wrote at 2024-3-15 09:13 +0000:

Hi. When starting a Plone 5.1.5 instance I get an ImportError in Zope2.Startup.init.py from "from zope.event import notify". I put a pdb just before the import call and below you can see it works the first time through but not the second:

This might be a namespace package problem:
formerly, namespace packages had to have an __init__.py
with either a pkg_resources.declare_namespace or a pkgutil.extend_path
call, declaring the package as a namespace package.
Modern Python3 versions now interpret a package without an __init__.py
as a namespace package.
When one namespace package (here zope) contains subpackage
distributions with mixed "init.py/no init.py" for the enclosing
namespace package, there can be import problems.

As far as I know, the zope namespace package should not yet have
switched to the new "no init.py means namespace package",
i.e. all zope.* subpackage distributions should still
have an __init__.py in the enclosing zope package
with a pkgresources.declare_namespace call.
I suggest to verify that this is indeed the case for
the zope.* subpackage distributions used in your instance.

I have seen similar problems, when
some subpackages of the same namespace package
have been installed in Python's site-packages while
others have been installed via buildout.

Since then, I try to install everything via buildout.

Many thanks @dieter! Removing all zope packages from Python's site-packages did the trick