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
  • 119,640
Vincent
  • 31

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,920