AWS boto3.session.Session() 和 boto3.Session() 之间的区别

Different between AWS boto3.session.Session() and boto3.Session()

我正在尝试使用 AWS python 库 boto3 创建会话。我发现我们可以做到

session = boto3.Session(profile_name='profile1')

session2 = boto3.session.Session(profile_name='profile2')

我检查了他们的 docs,它应该使用 boto3.session.Session()

为什么这两种方式都有效?他们背后的概念有什么不同?

只是为了方便; 它们指的是同一个 class。这里发生的是 python boto3 包的 __init__.py 包括以下内容:

from boto3.session import Session

这只允许您在 python 代码中将会话 class 称为 boto3.Session 而不是 boto3.session.Session。

这个 article 提供了关于这个 python 成语的更多信息:

One common thing to do in your __init__.py is to import selected Classes, functions, etc into the package level so they can be conveniently imported from the package.