Ben Mayo Ben Mayo - 10 days ago 6
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

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

Flask<1


At this stage the site loads fine.

If I then include an additional line
https://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.1.14.tar.gz

As per this answer http://stackoverflow.com/a/34489738/2697874

Then in my views.py file (which seems to be broadly synonymous to my old app.py 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

Per my experience, the resoure https://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.1.14.tar.gz 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.

Comments