Azure-functions-host: Azure Function '_mysql' is not defined Python3.8

Created on 25 Jul 2020  路  3Comments  路  Source: Azure/azure-functions-host

Check for a solution in the Azure portal

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

Investigative information

Please provide the following:

  • Timestamp: 2020-07-24 23:40:31.303
  • Function App version: 3
  • Function name(s) (as appropriate): vm
  • Invocation ID: 6a34ec330406364bb239b52b1bc84abd
  • Region: East US 2

Repro steps

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()

Expected behavior

Function correctly connecting to DB as in local environment. The DB is hosted on Azure

Actual behavior

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__

Known workarounds

Provide a description of any known workarounds.

Related information

Python 3.8
HTTP Triggered Function
Azure MySQL Database
SQLAlchemy

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-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

All 3 comments

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

Was this page helpful?
0 / 5 - 0 ratings