我正在尝试使用Python 3将AWS中EC2实例的启动时间日期更改为更友好的名称。
我得到的错误提示:
datetime(launch_time)
TypeError: 'module' object is not callable
我的程序正在这样做:
import boto3
import time
import datetime
instance_id = 'i-024b3382f94bce588'
instance = ec2.describe_instances(
InstanceIds=[instance_id]
)['Reservations'][0]['Instances'][0]
launch_time = instance['LaunchTime']
datetime(launch_time)
launch_time_friendly = launch_time.strftime("%B %d %Y")
print("Server was launched at: ", launch_time_friendly)
如何获取实例创建为用户友好格式的时间?
答案 0 :(得分:2)
同时有一个datetime
模块和一个datetime
类。您正在尝试调用该模块:
import datetime
dt = datetime(2019, 3, 1) # This will break!
相反,您需要从模块中导入类 :
from datetime import datetime
dt = datetime(2019, 3, 1) # Okay!
...或导入模块并引用该类:
import datetime
dt = datetime.datetime(2019, 3, 1) # Good!