Ben Mayo Ben Mayo - 1 year ago 85
Python Question

Importing mysql.connector into Azure Flask Project

I'm trying to deploy a Flask web app with mysql connectivity. It's my first time using Azure, and coming off Linux it all seems pretty confusing.

My understanding is that one includes within the

to include the packages required. When I build the default Flask app from Azure the file looks like this:


At this stage the site loads fine.

If I then include an additional line

As per this answer

Then in my file (which seems to be broadly synonymous to my old file) I include...
import mysql.connector

I then restart and reload my site...which then returns the error
The page cannot be displayed because an internal server error has occurred.

Error logging spits out a load of html (seems pretty weird way to deliver error logs - so I must be missing something here). When I save to html and load it up I get this...

enter image description here

How can I include the mysql.connector library within my Flask web app?

Answer Source

Per my experience, the resoure is for Linux, not for Azure WebApps based on Windows, and the link seems to be not available now.

I used the command pip search mysql-connector to list the related package. Then, I tried to use mysql-connector instead of mysql-connector-python via pip install, and tried to import mysql.connector in local Python interpreter that works fine.

So please use mysql-connector==2.1.4 instead of mysql-connector-python== in the requirements.txt file of your project using IDE, then re-deploy the project on Azure and try again. The package will be installed automatically as the offical doc said as below.

Package Management
Packages listed in requirements.txt will be installed automatically in the virtual environment using pip. This happens on every deployment, but pip will skip installation if a package is already installed.

Any update, please feel free to let me know.