1

I'm trying to run a python discord bot when my Raspberry Pi starts up. To do this, I've used a systemd service:

[Unit]
Description=Testing

[Service]
Type=idle
WorkingDirectory=/home/pi
ExecStart=/usr/bin/python3 /home/pi/discord/bug.py

[Install]
WantedBy=multi-user.target

I have done several test using simpler python programs before, and it all works fine. When trying to run the discord bot, it throws an error at the import statement. For testing I run:

sudo systemctl start bugstart
sudo systemctl status bugstart

The output of the status is the following:

bugstart.service - Testing
   Loaded: loaded (/lib/systemd/system/bugstart.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2018-08-12 02:08:47 UTC; 1s ago
  Process: 1039 ExecStart=/usr/bin/python3 /home/pi/discord/bug.py (code=exited, status=1/FAILURE)
 Main PID: 1039 (code=exited, status=1/FAILURE)

Aug 12 02:08:46 raspberrypi systemd[1]: Started Testing.
Aug 12 02:08:47 raspberrypi python3[1039]: Traceback (most recent call last):
Aug 12 02:08:47 raspberrypi python3[1039]:   File "/home/pi/discord/bug.py", line 1, in <module>
Aug 12 02:08:47 raspberrypi python3[1039]:     import discord
Aug 12 02:08:47 raspberrypi python3[1039]: ImportError: No module named 'discord'
Aug 12 02:08:47 raspberrypi systemd[1]: bugstart.service: Main process exited, code=exited, status=1/FAILURE
Aug 12 02:08:47 raspberrypi systemd[1]: bugstart.service: Unit entered failed state.
Aug 12 02:08:47 raspberrypi systemd[1]: bugstart.service: Failed with result 'exit-code'.

I found out that the commands are run as root, so I figured it might be that the library hand't been installed on root, but I tried importing discord in the command line shell and it worked fine.

  • 1
    The commands are not run by root, but by User=id which is the user you listed in your service file. If you import discord and import sys, what does sys.modules['discord'] show you? – filbranden Aug 12 '18 at 5:04
  • It's possible that Python imports are getting confused by the discord directory under /home/pi, which is the WorkingDirectory of your service... – filbranden Aug 12 '18 at 5:05
  • @FilipeBrandenburge That was a mistake on my part, I forgot to remove the User= I was just temporarily testing with it. – Aeolus Aug 12 '18 at 13:21
2

Systemd starts the processes with a minimal environment. In this case you are probably missing PYTHONPATH and maybe more. Try it yourself on the command line with env:

env -i /usr/bin/python3 /home/pi/discord/bug.py

You will probably get the same error. So now try it with

env -i PYTHONPATH=you-path /usr/bin/python3 /home/pi/discord/bug.py

If this works, add

Environment=PYTHONPATH=you-path

to your systemd unit.

If it doesn't work, you need to find out what else is needed.

  • The first one worked, but that might be because of another solution I recently tried. Someone told me to install the discord library with sudo -H pip3 install discord.py and that solved my problem. – Aeolus Aug 12 '18 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.