【发布时间】:2017-05-09 06:29:01
【问题描述】:
我正在尝试在 Bazel 构建的项目中使用 Boto3,但似乎无法正确导入该库。由于 Boto git 存储库,所有源代码都位于存储库根目录中名为 botocore 和 boto3 的文件夹中。导入都是boto3.boto3,第一个对应外部依赖项的名称,第二个是驻留的根文件夹。如何使用py_binary 和py_library 规则的imports 属性从内部boto3 而不是另一个导入?
这是我的工作区的样子:
//WORKSPACE
BOTOCORE_BUILD_FILE = """
py_library(
name = "botocore",
srcs = glob([ "botocore/**/*.py" ]),
imports = [ "botocore" ],
visibility = [ "//visibility:public" ],
)
"""
_BOTO3_BUILD_FILE = """
py_library(
name = "boto3",
srcs = glob([ "boto3/**/*.py" ]),
imports = [ "boto3" ],
deps = [ "@botocore//:botocore" ],
visibility = [ "//visibility:public" ],
)
"""
new_git_repository(
name = "botocore",
commit = "cc3da098d06392c332a60427ff434aa51ba31699",
remote = "https://github.com/boto/botocore.git",
build_file_content = _BOTOCORE_BUILD_FILE,
)
new_git_repository(
name = "boto3",
commit = "8227503d7b1322b45052a16b197ac41fedd634e9", # 1.4.4
remote = "https://github.com/boto/boto3.git",
build_file_content = _BOTO3_BUILD_FILE,
)
//BUILD
py_binary(
name = "example",
srcs = [ "example.py" ],
deps = [
"@boto3//:boto3",
],
)
//example.py
import boto3
boto3.client('')
检查构建文件夹的内容
$ ls bazel-bin/example.runfiles/*
bazel-bin/example.runfiles/__init__.py bazel-bin/example.runfiles/MANIFEST
bazel-bin/example.runfiles/boto3:
boto3 __init__.py
bazel-bin/example.runfiles/botocore:
botocore __init__.py
当我尝试运行示例脚本时,我得到AttributeError: 'module' object has no attribute 'client' 我可以import boto3.boto3 但随后使用其中的任何内容都会导致缺少依赖项,例如boto3.sessions,因为所有内容都嵌套在<target-name>.boto3 中
【问题讨论】:
-
from boto3 import boto3工作吗?