NameError: name '_mysql' is not defined when trying to use mysql functions in aws lambda in python language

0

My aws lambda function based on python 3.9 language failed with this error message.
NameError: name '_mysql' is not defined

I have uploaded the function code using s3. pip freeze gives me this list.

aiofiles==0.8.0
aiomysql==0.0.22
aiosqlite==0.17.0
anyio==3.5.0
asgiref==3.5.0
asyncpg==0.25.0
attrs==21.4.0
bcrypt==3.2.0
certifi==2021.10.8
cffi==1.15.0
cfgv==3.3.1
charset-normalizer==2.0.12
click==8.0.4
configparser==5.2.0
databases==0.5.5
distlib==0.3.4
dnspython==2.2.1
ecdsa==0.17.0
email-validator==1.1.3
emoji==1.7.0
fastapi==0.75.0
filelock==3.6.0
greenlet==1.1.2
h11==0.13.0
identify==2.4.12
idna==3.3
iniconfig==1.1.1
Jinja2==3.0.3
mangum==0.14.1
MarkupSafe==2.1.1
mysql-connector-python==8.0.28
mysqlclient==2.1.0
natsort==8.1.0
nodeenv==1.6.0
packaging==21.3
passlib==1.7.4
platformdirs==2.5.1
pluggy==1.0.0
pre-commit==2.17.0
protobuf==3.19.4
psycopg2-binary==2.9.3
py==1.11.0
pyasn1==0.4.8
pycparser==2.21
pydantic==1.9.0
PyMySQL==0.9.3
pymysql-utils==2.1.5
pyparsing==3.0.7
pytest==7.1.1
python-dotenv==0.19.2
python-jose==3.3.0
python-multipart==0.0.5
PyYAML==6.0
requests==2.27.1
rsa==4.8
seedir==0.3.0
six==1.16.0
sniffio==1.2.0
SQLAlchemy==1.4.32
starlette==0.17.1
toml==0.10.2
tomli==2.0.1
typing_extensions==4.1.1
urllib3==1.26.9
uvicorn==0.17.6
virtualenv==20.13.4

Am I missing any package? I am using sqlalchemy. Please help me.

asked 2 years ago1237 views
1 Answer
1

There's not a lot to go on here - for example, it's impossible to tell whether there is a library missing or it's some other dependency so I can only give generic advice.

If you are using Python packages in your Lambda the best thing you can do is package them (those packages/dependencies) in the function. There are a couple of ways of doing this: Using a ZIP file or using a container image - use whichever is most comfortable for you - if you haven't had experience with containers then the ZIP file is probably the way to go.

Note that you should create the ZIP on an Amazon Linux 2 instance in order to get package for the appropriate architecture that is compatible with Lambda. And with some careful coding (or by creating a separate Python utility) you will be able to test that your packaging is working correctly prior to putting it into a ZIP and uploading to Lambda.

In my experience, ensuring that all the packages are included with your function handler is the best way to avoid the issue you're having.

profile pictureAWS
EXPERT
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions