Connecting to MySQL using Python
Before you can access MySQL databases using Python, you must install one (or more) of the following packages in a virtual environment:- mysqlclient: This package contains the MySQLdb module. It is written in C, and is one of the most commonly used Python packages for MySQL.
- mysql-connector-python: This package contains the mysql.connector module. It is written entirely in Python.
- PyMySQL: This package contains the pymysql module. It is written entirely in Python.
Setting up the Python virtual environment and installing a MySQL package
To set up the Python virtual environment and install a MySQL package, follow these steps:- Log in to your account using SSH.
-
At the command prompt, type the following command:
-
To create a virtual environment, type one of the following commands:
-
If you are running Python 3.x, type the following command:
-
If you are running Python 2.x, type the following command:
📘 Note Subsequent commands in this procedure assume that the environment is named sqlenv. You can use any environment name you want, but make sure you replace all occurrences of sqlenv with your own environment name.
-
If you are running Python 3.x, type the following command:
-
To activate the virtual environment, type the following command:
📘 Note The command prompt now starts with (sqlenv) to indicate that you are working in a Python virtual environment. All of the following commands in this procedure assume that you are working within the virtual environment. If you log out of your SSH session (or deactivate the virtual environment by using the deactivate command), make sure you reactivate the virtual environment before following the steps below and running the sample code.
-
To update pip in the virtual environment, type the following command:
-
Type the command for the package you want to install:
-
To install the mysqlclient package, type the following command:
-
To install the mysql-connector-python package, type the following command:
-
To install the pymysql package, type the following command:
-
To install the mysqlclient package, type the following command:
Code sample
After you install a MySQL package in the virtual environment, you are ready to work with actual databases. The following sample Python code demonstrates how to do this, as well as just how easy it is to switch between the different SQL package implementations. The sample code works with Python 2.7 and Python 3.x. In your own code, replace username with the MySQL database username, password with the database user’s password, and dbname with the database name:- As you can see, Python’s portable SQL database API makes it very easy to switch between MySQL modules in your code. In the sample above, the only code changes necessary to use a different module are to the import and connect statements.
- If you are running an application that you created in the cPanel Python Selector, you may have to change the first line of the code above. Instead of using #!/usr/bin/python to invoke the interpreter, make sure the path references the Python executable in the virtual environment. For example:
More information
- For more information about Python’s portable SQL database API, please visit https://www.python.org/dev/peps/pep-0249.
- For more information about the mysqlclient package, please visit https://pypi.org/project/mysqlclient.
- For more information about the mysql-connector-python package, please visit https://pypi.python.org/pypi/mysql-connector-python.
- For more information about the PyMySQL package, please visit https://pypi.python.org/pypi/PyMySQL.