Python flask.ext.mysql is deprecated?

2019-01-15 10:35发布

问题:

When I run from flask.ext.mysql import MySQL I get the warning Importing flask.ext.mysql is deprecated, use flask_mysql instead.

So I installed flask_mysql using pip install flask_mysql,installed it successfully but then when I run from flask_mysql import MySQL I get the error No module named flask_mysql. In the first warning I also get Detected extension named flaskext.mysql, please rename it to flask_mysql. The old form is deprecated. .format(x=modname), ExtDeprecationWarning. Could you please tell me how exactly should I rename it to flask_mysql? Thanks in advance.

回答1:

flask.ext. is a deprecated pattern which was used prevalently in older extensions and tutorials. The warning is telling you to replace it with the direct import, which it guesses to be flask_mysql. However, Flask-MySQL is using an even more outdated pattern, flaskext.. There is nothing you can do about that besides convincing the maintainer to release a new version that fixes it. from flaskext.mysql import MySQL should work and avoid the warning, although preferably the package would be updated to use flask_mysql instead.



回答2:

flask.ext.X is the old form to import a Flask extension, it is deprecated since Flask v0.10. The new way is to use flask_X. That's why you got the first warning.

But apparently, Flask-MySQL does not update it's name form and use the flaskext as the package name (chedck it on GitHub). That's why you got the second warning.