I am trying to run a python script from a Linux bash script on a raspberrypi0 using raspbian.
When I run the python script alone, it works fine. When I run the script through a bash script, I get the following error:
Traceback (most recent call list):
File "sendUpdateQuery.py", line 3, in <module>
import mysql.connector
ImportError: No module named mysql.connector
When I run the python script alone as such: ./sendUpdateQuery.py it runs and works as expected. But as soon as I try to run the same python script from the following bash script, I get the aforementioned error.
testSS.sh
#! /bin/bash
sudo python sendUpdateQuery.py "INSERT INTO <tablename> (col1, col2, col3) VALUES (v1, v2, v3);"
sendUpdateQuery.py:
#! /usr/bin/env python3
import mysql.connector
import smtplib
import sys
def main():
# import mysql.connector
cnx = mysql.connector.connect(user='user', password='<password>', host='<ip-address>', database='<database>
try:
cursor = cnx.cursor()
cursor.execute(sys.argv[1])
finally:
cnx.commit()
cnx.close()
if __name__ == "__main__":
main()
I made sure that both files are enabled to be executed using sudo chmod +x sendUpdateQuery.py and sudo chmod +x testSS.sh.
I tried importing the mysql.connector in the main function of the program. But I got the same error. Just on a different line.
I know I have mysql.connector installed. I installed both of the following modules:
pip3 install mysql-connector
sudo apt-get install python3-mysql.connector
Still no dice.
mysql-connector. Please specify full path of the python. First manually check by running python and importing module.python, and in second placepython3, they might be 2 different python interpreterssudo python -m pip list | grep mysqland see if package is there or not. This is most likely issue with two python version installed on your os and you are using wrong one to execute your script.