[App Service For Linux(Function) ] Python ModuleNotFoundError: No module named 'MySQLdb'

created at 08-27-2021 views: 20

Problem Description

When using Azure Function to create a new Python Function, ModuleNotFoundError: No module named'MySQLdb' appears when using MySQLdb to connect to the database,

But we can find this module by looking at the Python Site-Package in the Function App, but why is there still an error that the module cannot be loaded?

Site-Package

problem analysis

Check that the currently used Python version is 3.8, so go to the 3.8 version directory and find that there is no pymysql module.

Searching for the keywords of Python 3 and pymysql on the Internet, I found that Python 3 does not support the mysqlbd module. Instead, use the pymysql module.

  • In python2, use pip install mysql-python to install and connect to the MySQL library, and import MySQLdb for use;
  • In python3, the connection library is changed to the pymysql library. Use pip install pymysql to install it, and import it directly to use.

Solution

Using the pymysql module, and adding the content of pymysql.install_as_MySQLdb() to the code, you can successfully access mysql.

import pymysql

pymysql.install_as_MySQLdb()
created at:08-27-2021
edited at: 08-27-2021: