Search code examples
pythonmysqlpython-3.xxamppmysql-python

How to use MYSql from xampp for python 3?


I am getting below error when I try to install MySQL client using the command "pip3 install mysqlclient".

Complete output from command python setup.py egg_info:
    /bin/sh: mysql_config: command not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/l1/f1klm_s92g53c9v2p1vrdwg80000gn/T/pip-install-a0t6svmj/mysqlclient/setup.py", line 18, in <module>
        metadata, options = get_config()
      File "/private/var/folders/l1/f1klm_s92g53c9v2p1vrdwg80000gn/T/pip-install-a0t6svmj/mysqlclient/setup_posix.py", line 53, in get_config
        libs = mysql_config("libs_r")
      File "/private/var/folders/l1/f1klm_s92g53c9v2p1vrdwg80000gn/T/pip-install-a0t6svmj/mysqlclient/setup_posix.py", line 28, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    OSError: mysql_config not found

XAMPP version:- 7.2.3.0 Python:- 3.7

Can anybody help me to solve this error? Thanks


Solution

  • To install the MySQL-python package, type the following command:

    pip install MySQL-python
    

    To install the mysql-connector-python package, type the following command:

    pip install mysql-connector-python
    

    To install the pymysql package, type the following command:

    pip install pymysql
    

    Code sample

    hostname = 'localhost'
    username = 'USERNAME'
    password = 'PASSWORD'
    database = 'DBNAME'
    def doQuery( conn ) :
    cur = conn.cursor()
    
    cur.execute( "SELECT fname, lname FROM employee" )
    for firstname, lastname in cur.fetchall() :
        print firstname, lastname
    print "Using MySQLdb…"
    import MySQLdb
    myConnection = MySQLdb.connect( host=hostname, user=username, passwd=password, db=database )
    doQuery( myConnection )
    myConnection.close()
    print "Using pymysql…"
    import pymysql
    myConnection = pymysql.connect( host=hostname, user=username, passwd=password, db=database )
    doQuery( myConnection )
    myConnection.close()
    print "Using mysql.connector…"
    import mysql.connector
    myConnection = mysql.connector.connect( host=hostname, user=username, passwd=password, db=database )
    doQuery( myConnection )
    myConnection.close()