1

I don't have much experience with Python, but I have made a script that reads temp and pressure and sends it to a mysql database. When I log in ssh and run the script manually everything works. But when I put the script to start at boot, it says:

pi@raspberrypi:~ $ Traceback (most recent call last):
  File "/home/pi/code1.py", line 1, in <module>
    import mysql.connector
ImportError: No module named mysql.connector

Same with another boot option on another raspberry pi:

systemctl status sample.service
● sample.service - My Sample Service
   Loaded: loaded (/etc/systemd/system/sample.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Thu 2019-07-11 13:49:32 CEST; 4min 35s ago
  Process: 555 ExecStart=/usr/bin/python /home/pi/code1.py (code=exited, status=1/FAILURE)
 Main PID: 555 (code=exited, status=1/FAILURE)

Jul 11 13:49:31 localhost systemd[1]: Started My Sample Service.
Jul 11 13:49:32 localhost python[555]: Traceback (most recent call last):
Jul 11 13:49:32 localhost python[555]:   File "/home/pi/code1.py", line 1, in <module>
Jul 11 13:49:32 localhost python[555]:     import mysql.connector
Jul 11 13:49:32 localhost python[555]: ImportError: No module named mysql.connector
Jul 11 13:49:32 localhost systemd[1]: sample.service: Main process exited, code=exited, status=1/FAILURE
Jul 11 13:49:32 localhost systemd[1]: sample.service: Failed with result 'exit-code'.

What am I doing wrong?

Eliah Kagan
  • 117,780
Vincent
  • 31
  • 1
    Are you running Raspbian or Ubuntu? – rtaft Jul 11 '19 at 12:22
  • And in either case did you install the Python libs containg the MySQL connector? – Thomas Ward Jul 11 '19 at 12:24
  • I had something similar: Python 'Import pyodbc' does not work in a cron task. The reason was that the package had been installed using another name. So, maybe the first thing to do is to create/modify your crontab/servvie defintion (I am not sure how you start the program at boot) to use the same userid in ssh and cron/service. – Marc Vanhoomissen Jul 11 '19 at 12:26
  • I am running Raspbian buster. I think the Mysql connector python libs are installed since it is working when I run it manually in ssh... – Vincent Jul 11 '19 at 12:29
  • Marc, I can try to modify the definition. Its like this: [Unit] Description=My Sample Service After=multi-user.target

    [Service] Type=idle ExecStart=/usr/bin/python /home/pi/code1.py

    [Install] WantedBy=multi-user.target

    – Vincent Jul 11 '19 at 12:30
  • post the output of pip --version and python --version – rtaft Jul 11 '19 at 12:55
  • pip --version pip 19.1.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7) python --version Python 2.7.16 – Vincent Jul 11 '19 at 13:02
  • I dont think I have cron. cron --version cron: invalid option -- '-' usage: cron – Vincent Jul 11 '19 at 13:08
  • Ok. systemd 241 (241) +PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 default-hierarchy=hybrid – Vincent Jul 11 '19 at 13:18
  • nm, the answer that I was looking for is in the original post. – rtaft Jul 11 '19 at 13:29
  • Instead of calling python directly in the service, create an .sh file, call your python script from that, and call the .sh script in your service. That will allow you to do a bit more debugging in the shell script. – rtaft Jul 11 '19 at 13:35

2 Answers2

2

After some testing with different commands. I finally got it to work. I had to install it globally like rtaft suggested. I did it with this command: sudo -H pip install mysql-connector

Thanks :)

Vincent
  • 31
0

Either it is installed in your home directory or you are using virtualenv. Install it globally.

sudo pip install mysql-connector-python

If you are using virtualenv, you need to wrap it in a shell script and activate it in that script, ie myscript.sh:

source ~/pathtoproject/venv/bin/activate
python ~/pathtoproject/myscript.py
rtaft
  • 1,825