使用Python Lambda连接MySQL RDS时遇PyMySQL依赖导入失败问题求助
Hey Igor, let's work through this issue step by step—both your local VS Code/Pylance error and the Lambda deployment failure stem from how dependencies are being managed. Here's how to fix them:
Fix the Local Pylance Import Error
Pylance can't find pymysql because it's either not looking at the right Python environment, or the dependency isn't installed in the environment you're using in VS Code:
Confirm your Python environment
- Open VS Code and check the Python version displayed in the bottom-left corner. Make sure it's the 3.8.9 version you installed earlier. If not, click the version, select "Python: Select Interpreter" from the command palette (
Cmd+Shift+P), and pick the 3.8.9 interpreter.
- Open VS Code and check the Python version displayed in the bottom-left corner. Make sure it's the 3.8.9 version you installed earlier. If not, click the version, select "Python: Select Interpreter" from the command palette (
Use a virtual environment (recommended)
- Create a virtual environment in your project folder to isolate dependencies:
python3.8 -m venv .venv - Activate it on macOS:
source .venv/bin/activate - Install PyMySQL inside the activated environment:
pip install pymysql - Reload VS Code, and Pylance should now resolve the import correctly.
- Create a virtual environment in your project folder to isolate dependencies:
Fix the Lambda Dependency Missing Error
Lambda can't find pymysql because your packaging process has an issue—either the dependency wasn't installed to the right location, or the zip file structure is incorrect for Lambda:
Correctly install dependencies to a target folder
- In your project root directory, create a
packagefolder to hold dependencies:mkdir package - Install PyMySQL directly into this folder using your 3.8.9 Python interpreter:
python3.8 -m pip install pymysql --target ./package
Note: Using
python3.8explicitly ensures you're installing for the same Python version Lambda uses (3.8), avoiding compatibility issues.- In your project root directory, create a
Package the code correctly
- Move your
app.pyfile into thepackagefolder. - Navigate into the
packagefolder, select all files and subfolders (includingpymysqlandapp.py), and compress them into a zip file. Do not zip thepackagefolder itself—Lambda needs the files at the root of the zip.
- Move your
Optional: Fix cross-platform dependency issues
- If you still get errors after packaging, macOS-native dependencies might not work on Lambda's Linux environment. Install Linux-compatible wheels with this command instead:
python3.8 -m pip install pymysql --target ./package --platform manylinux2014_x86_64 --only-binary=:all:
- If you still get errors after packaging, macOS-native dependencies might not work on Lambda's Linux environment. Install Linux-compatible wheels with this command instead:
Quick Code Fix
I noticed a small issue in your app.py that will cause an error once the dependency is resolved:
# Your current line (has an unused parameter) cursor.execute(sql, ('webmaster@python.org',)) # Change it to this (no parameters needed for your SELECT query) cursor.execute(sql)
Your SQL query doesn't have any placeholders, so passing a parameter tuple will throw a "number of arguments doesn't match" error.
内容的提问来源于stack exchange,提问作者Igor Karpenko

