首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Runtime.ImportModuleError:无法导入模块“索引”:没有名为“pg8000”的模块

Runtime.ImportModuleError:无法导入模块“索引”:没有名为“pg8000”的模块
EN

Stack Overflow用户
提问于 2019-11-24 16:40:20
回答 1查看 4.7K关注 0票数 4

我部署了一个lambda (python3.7),它使用pg8000

代码语言:javascript
复制
import pg8000
..
def get_connection():
    """
        Method to establish the connection.
    """
    try:
        print ("Connecting to database")
        # Create a low-level client with the service name for rds
        client = boto3.client("rds")
        # Read the environment variables to get DB EndPoint
        DBEndPoint = os.environ.get("DBEndPoint")
        # Read the environment variables to get the Database name
        DatabaseName = os.environ.get("DatabaseName")
        # Read the environment variables to get the Database username which has access to database.
        DBUserName = os.environ.get("DBUserName")
        # Generates an auth token used to connect to a db with IAM credentials.
        password = client.generate_db_auth_token(
            DBHostname=DBEndPoint, Port=5432, DBUsername=DBUserName
        )
        # Establishes the connection with the server using the token generated as password
        conn = pg8000.connect(
            host=DBEndPoint,
            user=DBUserName,
            database=DatabaseName,
            password=password,
            ssl={"sslmode": "verify-full", "sslrootcert": "rds-ca-2015-root.pem"},
        )
        print("Succesful connection!")
        return conn
    except Exception as e:
        print ("While connecting failed due to :{0}".format(str(e)))
        return None
...

我有一个requirements.txt,它包含:

代码语言:javascript
复制
pg8000==1.13.2
boto3==1.9.67

我正在执行一个山姆构建,山姆包和山姆部署。sam不处理像pg8000这样的依赖项的下载吗?

确切的命令:

代码语言:javascript
复制
sam build
sam package --template-file ./template.yml --output-template-file output.yml --s3-bucket xxx-bucket
sam deploy --template-file ./output.yml --stack-name demo --capabilities CAPABILITY_IAM

触发lambda后出错:

代码语言:javascript
复制
[ERROR] Runtime.ImportModuleError: Unable to import module 'index': No module named 'pg8000'
EN

回答 1

Stack Overflow用户

发布于 2019-11-26 06:05:46

根据医生

sam 查找包含依赖项的清单文件(如requirements.txt),并自动创建部署构件。

您的模块似乎根本没有部署。

我将解释如何一步一步地修复它:

  • sam build创建了一个包含lambda函数的文件夹.aws-sam/build,其中包括依赖项和一个非常重要的文件:相对于.aws-sam/build文件夹,使用带有CodeUri template.yml ( lambdas代码的路径)。
  • sam package命令应该将template.yml的位置作为参数传递,这意味着sam构建目标文件夹url。运行sam package --template-file .\.aws-sam\build\template.yml --output-template-file .\.aws-sam\build\output.yml --s3-bucket your_bucket
  • 最后运行sam deploy --template-file .\.aws-sam\build\output.yml --stack-name demo --capabilities CAPABILITY_IAM

执行以下命令:sam package --template-file ./template.yml --output-template-file output.yml --s3-bucket xxx-bucket时,模板文件./template.yml指向没有位于.\.aws-sam\build\your_lambda.中的依赖项

还有一件事:

如果您正在使用多个模块,则应考虑使用

希望这能有所帮助

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59019965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档