简体   繁体   中英

Bash script can't find Python MySQL.Connector Module

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.

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);"

Try to install the mysql-connector with:

pip install mysql-connector-python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM