在Python Bazel中,如果你想从另一个目标定义的模块中导入代码,可以按照以下步骤操作:
project/
├── WORKSPACE
├── BUILD
├── module1/
│ └── __init__.py
│ └── module1.py
└── module2/
└── __init__.py
└── module2.py
WORKSPACE
文件中,添加必要的依赖项和规则。例如,如果你的项目依赖于module1
和module2
,可以这样写:load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "module1",
remote = "https://github.com/your_username/module1.git",
commit = "abcdef1234567890",
)
git_repository(
name = "module2",
remote = "https://github.com/your_username/module2.git",
commit = "1234567890abcdef",
)
BUILD
文件中,为每个模块定义一个规则。例如,对于module1
,可以这样写:py_library(
name = "module1",
srcs = glob(["module1/*.py"]),
deps = [
"@module2//:module2",
],
)
注意,这里使用了@module2//:module2
来指定从module2
模块中导入的代码。
import
语句导入模块。例如,在module2.py
中,可以这样导入module1
模块:from module1 import module1
这样,你就可以在module2.py
中使用module1
模块中的代码了。
以上就是使用Python Bazel从另一个目标中定义的模块导入的解决方法,希望对你有帮助!