1

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.

Cœur
  • 32,421
  • 21
  • 173
  • 232
Reinkor
  • 21
  • 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. – Poojan Nov 15 '19 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: https://stackoverflow.com/questions/2429511/why-do-people-write-the-usr-bin-env-python-shebang-on-the-first-line-of-a-pyt and remember if you write in one place `python`, and in second place `python3`, they might be 2 different python interpreters – 404pio Nov 15 '19 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. – Poojan Nov 15 '19 at 21:23
  • Possible duplicate of [Import mysql.connector Python](https://stackoverflow.com/q/24272223/608639), [ImportError: No module named mysql.connector using Python2](https://stackoverflow.com/q/24272223/608639), [ImportError: No module named mysql.connector using Python3?](https://stackoverflow.com/q/20275176/608639), etc. – jww Nov 15 '19 at 21:42

1 Answers1

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);"
Reinkor
  • 21
  • 4