Plone API missing module 'event'

I'm using the Plone API to create objects on an (standalone) instance of Plone 5.1 (using the standard installer). In my script to create the objects, I need to add a datetime to the "effective" property ('Publication Date') in the object to be created. In order to do so, I needed to install the datetime module, which I did via pip in the /bin folder. That went all well and good. However, when I ran the code, I got an error message.

Here are the import statements in my python script:

from bs4 import BeautifulSoup
import urllib
import os, json
from datetime import datetime
from plone import api
import transaction
from plone.app.textfield.value import RichTextValue 
import plone.app.event

... thereafter, I set the effective field to datetime.now() or whatever the input data requires.

This is what I get when I run the code...

c@lancer:/opt/plone/zinstance/bin$ sudo -u plone_daemon ./instance --object-path="/test" run ./TexAG_Uploader.py

Traceback (most recent call last):
File "/opt/plone/zinstance/parts/instance/bin/interpreter", line 293, in
exec(_val)
File "", line 1, in
File "/opt/plone/buildout-cache/eggs/Zope2-2.13.27-py2.7.egg/Zope2/init.py", line 60, in
from Zope2.Startup.run import configure
File "/opt/plone/buildout-cache/eggs/Zope2-2.13.27-py2.7.egg/Zope2/Startup/init.py", line 27, in
from zope.event import notify
ImportError: No module named event

I would have thought that import plone.app.event would have fixed this problem, but it didn't. Does anyone know what I need to import/install/copy to fix this problem? Obviously, I'm missing a module called event, but which code has that particular event module? I just need to know what to import.

There is more here than meets the eye. Something about installing the datetime module via pip throws Plone for a loop.

I am (want to?) use Plone API to load tens of thousands of documents that are in (individual) json files. As I mentioned in my last post, I need to change the effective property (which is a datetime object). I ran into the problem of the datetime module being missing when running the python script. To remedy that problem, I ran pip2.7 install datetime as plone_buildout. E.g.,

sudo -H -u plone_buildout ./pip2.7 install datetime

Collecting datetime
Downloading https://files.pythonhosted.org/packages/73/22/a5297f3a1f92468cc737f8ce7ba6e5f245fcfafeae810ba37bd1039ea01c/DateTime-4.3-py2.py3-none-any.whl (60kB)
100% |████████████████████████████████| 61kB 712kB/s
Collecting pytz (from datetime)
Downloading https://files.pythonhosted.org/packages/30/4e/27c34b62430286c6d59177a0842ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl (510kB)
100% |████████████████████████████████| 512kB 1.4MB/s
Collecting zope.interface (from datetime)
Downloading https://files.pythonhosted.org/packages/cd/14/55694f6289425d1cd41c59cf0aaf0188686b5b544140cab64c6f9e508751/zope.interface-4.5.0-cp27-cp27mu-manylinux1_x86_64.whl (163kB)
100% |████████████████████████████████| 163kB 2.4MB/s
Requirement already satisfied: setuptools in /opt/plone/zinstance/lib/python2.7/site-packages (from zope.interface->datetime) (38.7.0)
Installing collected packages: pytz, zope.interface, datetime
Successfully installed datetime-4.3 pytz-2018.5 zope.interface-4.5.0

Like I said, it installed successfully, but that was all it took to knock the Plone installation out of commission.

Note, as an experiment, and just to make everything as pristine as possible, I started out with an Ubuntu 16.04 (sever) clean install, and then installed Plone (5.1) from the unified installer (standalone, because I'm doing development, with all recommended dependencies per the Plone installation instructions). Then, just to make sure all was well, I started Plone and created a Plone site. All was well. No problems.

Next, I ran the pip2.7 install datetime and restarted Plone. A process stared, but I could no longer access my Plone site (e.g., http://localhost:8080/ did not return a response).

I'm at a loss to know what went wrong. Can someone duplicate this experiment and confirm the same problem?

I don't follow exaclty why you are having problems with importing the event module, but something else that for me signals a "where there's smoke there's a fire", you describe that you have to import the datetime module to set datetimes on Plone objects. Not really:

Are you aware that you should not use datetime.datetime but DateTime? Some of Zope's first internal data structures predate modern python (standard libraries), they just weren't there yet. You can find more information here:

https://docs.plone.org/develop/plone/misc/datetime.html

As an example, to change the modification date on a series of objects I update in a project synced from an xml file I do:

                            import DateTime

                            now = DateTime.DateTime()
                            obj.setModificationDate(now)
                            obj.reindexObject(idxs=['modified'])

Well it isn't that simple. An Event start and end date has to be a non-naive python datetime. I hate that this is mixed, but due to migration problems we never managed to harmonize this to use the python implementation all over the place. That's if you work with an old code base.

Have you tried importing it like this

from plone.app import event

Lunga1: That was the first thing that I (it seemed like the obvious thing to do), but it didn't work. (It could never find the "event" module.) That problem arose when I installed datetime (not DateTime). Consequently, the "missing" event module was a consequence of installing datetime and using that instead of DateTime.

Fred: For posterity, from a plone.api standpoint, you need to:

from DateTime import DateTime

And then when you go about using it, you need to do something like...

effective_date = DateTime.DateTime('2017/3/9 1:45pm')

That appears to work.

Thanks for the help!

Just to clarify, datetime its a python module, it comes with python you dont need to install it. DateTime it is a zope library/module it was created before python had its own datetime. They don't replace each other.

Plone its build on top of Zope, and nowadays some attributes are datetime and some DateTime.

As in the other post you asked about how to set some fields, i wrote there that code

from DateTime import DateTime
DateTime(datetime.datetime.now())

Some DateTime fields are effective, creation, modification and expiration dates. While, as Jens said, event dates are python aware datetime. Also take a look at the link Fred provided if you need conversions between each other

1 Like