1

I am working on a data acquisition project where I am sending a bunch of sensor data to the cloud. Here is a small snippet from my code:

#!/usr/bin/python3
# coding: utf-8

import ctypes
from systemd import journal
.
.
.#Rest of the code
.
journal.send(
        channel = 'sensor',
        priority = journal.Priority.INFO,
        messageid = "%d" % (seq_num),
        timestamp = "%s" % (CurrentTimestamp),
        tdiff = "%s" % (dt),     
        acc_x="%f" % (cax),
        acc_y="%f" % (cay),
        acc_z="%f" % (caz),
        gyr_x="%f" % (cgx),
        gyr_y="%f" % (cgy),
        gyr_z="%f" % (cgz),
        horn = "%d" % (GPIO.input(hornpin)),
        brake = "%d" % (GPIO.input(brakepin)),
        rpm = "%d" % (GPIO.input(rpmpin)),
        lux = "%f" % (lux),
        temperature_batterypack = "%d" % (temperature_batterypack),
        temperature_ambientair = "%d" % (temperature_ambientair),
        temperature_scooterbody = "%d" % (temperature_scooterbody),
        )

I have designed my own board using the public schematics of Raspberry Pi Compute module 3. The OS that I am using is the latest version of Raspbian Stretch Lite (June 2018 release). I am trying to autostart the code at boot by adding the following line in /etc/rc.local before exit 0:

sudo python3 /home/pi/test_scripts/cloud_test.py &

and I get the following error:

AttributeError: module 'systemd.journal' has no attribute 'Priority'

I even tried autostarting using systemd (so I created a service file) and when I check the status of the service file, it gives me the same exact error. Whereas, if I don't do auto start and manually run the code on the command prompt as:

$ python3 cloud_test.py

the code runs correctly and sends data to the cloud and I can see all the data on a website. This means all modules and python packages are correctly installed and I do not get any error. I also made by python script executable by using sudo chmod +x cloud_test.py and have also included #!/usr/bin/python3 at the top of my code. Then why do I see the error AttributeError: module 'systemd.journal' has no attribute 'Priority' only on autostart but not on manual start?

Aurora0001
  • 6,308
  • 3
  • 23
  • 38
  • Have a look at you Pi forum topic: https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=220304#p1352270 – Dirk Aug 13 '18 at 09:16
  • @Dirk: I think the OP here was also the OP on that thread. – Seamus Aug 13 '18 at 16:59
  • @Seamus I know, I didn't feel like writing it twice if this is not the cause. Fairly certain it is though. – Dirk Aug 13 '18 at 17:06
  • Oh - OK... I assumed the OP would have followed up on that, and updated his post here if it had any value. Possibly a bad assumption on my part :) – Seamus Aug 13 '18 at 17:13
  • Umm probably silly on my part but what's OP? – Sohil Mehta Aug 13 '18 at 18:47
  • @SohilMehta No need to edit to add [SOLVED] to your title; just click the checkmark next to the answer which solved your problem (or you can post your own answer as you have done if none of the ones below solved your issue). You'll be able to mark your answer as accepted in a couple of days, but no need to edit your title in the meantime. Glad to hear you solved your issue. – Aurora0001 Aug 13 '18 at 18:57
  • 1
    @Aurora0001 It says I can click the check mark only after two days. Cool, will do that then. – Sohil Mehta Aug 13 '18 at 19:02

2 Answers2

1

I have copy and pasted your test code and runs it from the command line:

rpi:~ $ ./cloud_test.py
Traceback (most recent call last):
  File "./cloud_test.py", line 12, in <module>
    priority = journal.Priority.INFO,
AttributeError: module 'systemd.journal' has no attribute 'Priority'
rpi:~ $

It seems there is no problem with starting the script in rc.local or with a systemd unit file. Instead there may be a problem with your test scripts. Do you really use the same script for bootup and for testing on the command line? Have a look at systemd.journal module what's with the attribute 'Priority'.

Ingo
  • 42,107
  • 20
  • 85
  • 197
  • Yes I use the same script for bootup and command line. When running the script manually from the command line, it works absolutely fine without any errors. – Sohil Mehta Aug 13 '18 at 09:11
  • What is the difference between my command line and yours? I use Raspbian Stretch Lite 2018-06-27 upgraded. Maybe your stderr is redirected? With ./cloud_test.py 2>/dev/null I also get no error message. – Ingo Aug 13 '18 at 09:19
  • re "what's with the attribute PRIORITY": From the link you provided: priority is the syslog priority, one of LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG. – Seamus Aug 13 '18 at 17:03
  • @Seamus I have seen that also. In a quick test with the copy/pasted program I tried journal.priority.INFO, journal.PRIORITY.INFO, journal.Priority, journal.priority and journal.PRIORITY with no avail. – Ingo Aug 13 '18 at 17:18
  • It seems you've been unable to replicate the problem - true? At any rate, the OP may have gotten an answer on another website - see @Dirk's comment to the question – Seamus Aug 13 '18 at 17:50
  • Hey guys, sorry I was a bit held up. Yes I got the answer. The problem was because, while installing the module systemd from pypi, I did pip3 install sytemd and not sudo pip3 install systemd. Thanks Ingo. – Sohil Mehta Aug 13 '18 at 18:31
0

This issue was solved by reinstalling systemd module as sudo pip3 install systemd

Intially I did not include sudo while installing systemd.

It can also be solved by including User=pi in the service file.

[Unit]
Description = MESSI v2.0 sensor code
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python3 -u cloud_test.py
WorkingDirectory=/home/pi/test_scripts
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

You can view the discussion here too: https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=220304&p=1352497#p1352497