Python-EC2对象中的Switch AWS Accounts没有属性“instances”错误

Python-EC2对象中的Switch AWS Accounts没有属性“instances”错误,python,boto,Python,Boto,我试图使用Python中的命名概要文件和boto3列出特定AWS帐户中的EC2实例 错误显示: File ".\aws_ec2_list_instance_info.py", line 18, in <module> running_instances = ec2.instances.filter(Filters=[{ File "C:\Users\tdunphy\AppData\Local\Programs\Python\Python37-32\lib\site-pac

我试图使用Python中的命名概要文件和boto3列出特定AWS帐户中的EC2实例

错误显示:

File ".\aws_ec2_list_instance_info.py", line 18, in <module>
    running_instances = ec2.instances.filter(Filters=[{
  File "C:\Users\tdunphy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\botocore\client.py", line 601, in __getattr__
    self.__class__.__name__, item)
AttributeError: 'EC2' object has no attribute 'instances'

我做错了什么?

似乎没有ec2客户端的实例函数。也许这就是你要找的


描述实例,

在此处查看boto3文档。
或者查看此

是否可以将您的问题包括在您使用的Boto 3版本中。当前的两个版本。1.9.108文件未将实例列为EC2.Client.Ok的方法。如何确定我正在使用的boto3的版本?好的,我可能会修改代码以使用descripe_instances。但是我注意到,如果我切换到boto3.resource,我的代码可以工作,而不需要切换帐户:ec2=boto3.resource'ec2'running\u instances=ec2.instances.filterFilters=[{'Name':'instance state Name','Values':['running']}]返回---名称:USAWSCDLX00061实例ID:i-0463897140af217f8类型:m4.大状态:运行私有IP:10.48.136.41公共IP:无启动时间:2017-12-20 14:26:30+00:00我有两个问题。boto3.resource和boto3.client之间有什么区别?有什么方法可以使用bot3.resource进行帐户切换吗?我将指向你这篇文章,它解释得很好,好吧,太好了!谢谢,我会查出来的。更新。当我将它切换到boto3资源而不是客户机时,这就起作用了。我可以切换帐户:session=bot3.Sessionprofile\u name=aws\u account ec2=session.resource'ec2'
from collections import defaultdict
import boto3
aws_account = input("Enter the name of the AWS account you'll be working in: ")
# Connect to EC2
session = boto3.Session(profile_name=aws_account)
ec2 = session.client('ec2')
# Get information for all running instances
running_instances = ec2.instances.filter(Filters=[{
    'Name': 'instance-state-name',
    'Values': ['running']}])