2

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.

4
  • Probably python you are executing script with is not the same python you installed your mysql-connector. Please specify full path of the python. First manually check by running python and importing module. Commented Nov 15, 2019 at 21:18
  • 1
    Probably you have 2 version of python in your computer. Python2 without mysql module, and python3 with mysql module. Look also at this question: stackoverflow.com/questions/2429511/… and remember if you write in one place python, and in second place python3, they might be 2 different python interpreters Commented Nov 15, 2019 at 21:19
  • If grep available on your system try running sudo python -m pip list | grep mysql and 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. Commented Nov 15, 2019 at 21:23
  • Possible duplicate of Import mysql.connector Python, ImportError: No module named mysql.connector using Python2, ImportError: No module named mysql.connector using Python3?, etc. Commented Nov 15, 2019 at 21:42

1 Answer 1

1

Thank you to those who made the comments in my post. @404pio and @Poojan. I have python2.7 and python 3 installed, and when I specified my call using python3, it worked! Thanks so much you two!

Code snippet that fixed my problem:

sudo python3 sendUpdateQuery.py "INSERT INTO <tablename> (col1, col2, col3) VALUES (v1, v2, v3);"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.