I'm trying to create some Python3 Azure MySQL jobs in azure functions using SQL Alchemy.
Function runs locally with func start without a problem.
It's in fresh venv, on fresh Linux VM to exclude any possible package dependencies.
Deploying to Azure via func azure functionapp publish {app_name} --build remote without any problems. But upon calling the function I'm getting:
"name '_mysql' is not defined"
It seems like the MySQLdb module is not installed, but my requirements.txt contains mysqlclient==2.0.1 and it's installing properly. Even weirder, it works great from func start when I'm running the function locally on windows and linux
Please provide the following:
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_utils import create_database, database_exists
from sqlalchemy.orm import sessionmaker
import logging
import os
conn_string = os.environ['azmonitdb_connstring']
url = '{0}?ssl_ca=BaltimoreCyberTrustRoot.crt.pem'.format(conn_string)
if not database_exists(url):
logging.info("Database not existing, creating")
create_database(url)
engine = create_engine(url)
Session = sessionmaker(bind=engine)
Base = declarative_base()
Function correctly connecting to DB as in local environment. The DB is hosted on Azure
This is the full error, sorry for the formatting:
Result: Failure Exception: NameError: name '_mysql' is not defined Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 262, in _handle__function_load_request func = loader.load_function( File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 32, in call return func(*args, **kwargs) File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/loader.py", line 76, in load_function mod = importlib.import_module(fullmodname) File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1014, in _gcd_import File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "<frozen importlib._bootstrap>", line 1014, in _gcd_import File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 671, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 783, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/home/site/wwwroot/vm/__init__.py", line 4, in <module> from __app__.vm.get_vm import insert_vms File "/home/site/wwwroot/vm/get_vm.py", line 3, in <module> from __app__.shared.db.models import VM File "/home/site/wwwroot/shared/db/models.py", line 3, in <module> from __app__.shared.db.base import Base, engine File "/home/site/wwwroot/shared/db/base.py", line 12, in <module> if not database_exists(url): File "/home/site/wwwroot/.python_packages/lib/site-packages/sqlalchemy_utils/functions/database.py", line 462, in database_exists engine = sa.create_engine(url) File "/home/site/wwwroot/.python_packages/lib/site-packages/sqlalchemy/engine/__init__.py", line 500, in create_engine return strategy.create(*args, **kwargs) File "/home/site/wwwroot/.python_packages/lib/site-packages/sqlalchemy/engine/strategies.py", line 87, in create dbapi = dialect_cls.dbapi(**dbapi_args) File "/home/site/wwwroot/.python_packages/lib/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 118, in dbapi return __import__("MySQLdb") File "/home/site/wwwroot/.python_packages/lib/site-packages/MySQLdb/__init__.py", line 24, in <module> version_info, _mysql.version_info, _mysql.__file__
Provide a description of any known workarounds.
Python 3.8
HTTP Triggered Function
Azure MySQL Database
SQLAlchemy
any update on this one?
using mysql-connector-python fixes it.
it is an implementation the MySQL Client/Server protocol completely in Python
pip install mysql-connection-python
and change connection string to
mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>
ref:
https://github.com/mysql/mysql-connector-python
https://docs.sqlalchemy.org/en/13/dialects/mysql.html#module-sqlalchemy.dialects.mysql.mysqlconnector
Closing this issue as there is a fix mentioned above. Please feel free to re-open the issue, if you are have any further queries regarding the above issue
Most helpful comment
using mysql-connector-python fixes it.
it is an implementation the MySQL Client/Server protocol completely in Python
pip install mysql-connection-pythonand change connection string to
mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>ref:
https://github.com/mysql/mysql-connector-python
https://docs.sqlalchemy.org/en/13/dialects/mysql.html#module-sqlalchemy.dialects.mysql.mysqlconnector