You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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:

  1. 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.
  2. 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.

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:

  1. Correctly install dependencies to a target folder

    • In your project root directory, create a package folder 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.8 explicitly ensures you're installing for the same Python version Lambda uses (3.8), avoiding compatibility issues.

  2. Package the code correctly

    • Move your app.py file into the package folder.
    • Navigate into the package folder, select all files and subfolders (including pymysql and app.py), and compress them into a zip file. Do not zip the package folder itself—Lambda needs the files at the root of the zip.
  3. 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:
      

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 18:22:33