我有两个git repos和python代码。

https://github.com/SurferJeffAtGoogle/synthtool/tree/bazel包含两个构建目标:

~/gitrepos/synthtool$ bazel query ...
//tests:synthtool_test
//synthtool:synthtool


https://github.com/SurferJeffAtGoogle/nodejs-vision/tree/bazel包含一个构建目标:

~/gitrepos/nodejs-vision$ bazel query ...
//:synth


//tests:synthtool_test//:synth都依赖于//synthtool:synthtool

我可以运行测试,并且测试代码成功导入synthtool

~/gitrepos/synthtool$ bazel test ...
INFO: Analyzed 2 targets (4 packages loaded, 230 targets configured).
INFO: Found 1 target and 1 test target...
INFO: Elapsed time: 7.477s, Critical Path: 7.07s
INFO: 2 processes: 2 linux-sandbox.
INFO: Build completed successfully, 3 total actions
//tests:synthtool_test                                                   PASSED in 7.0s

Executed 1 out of 1 test: 1 test passes.


但是,当我尝试运行//:synth时,无法导入synthtool

~/gitrepos/nodejs-vision$ bazel run //:synth
Starting local Bazel server and connecting to it...
INFO: Analyzed target //:synth (17 packages loaded, 272 targets configured).
INFO: Found 1 target...
INFO: Deleting stale sandbox base /usr/local/google/home/rennie/.cache/bazel/_bazel_rennie/dadb642c5feb5ae86ab3
1dcb8a48c54a/sandbox
Target //:synth up-to-date:
  bazel-bin/synth
INFO: Elapsed time: 2.555s, Critical Path: 0.03s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Traceback (most recent call last):
  File "/usr/local/google/home/rennie/.cache/bazel/_bazel_rennie/dadb642c5feb5ae86ab31dcb8a48c54a/execroot/nodejs_vision/bazel-out/k8-fastbuild/bin/synth.runfiles/nodejs_vision/synth.py", line 18, in <module>
    import synthtool.gcp as gcp
ModuleNotFoundError: No module named 'synthtool.gcp'
rennie@rennie:~/gitrepos/nodejs-vision$


这两个目标具有完全相同的代码行:

import synthtool.gcp as gcp


它在测试中成功,但在//:synth中未成功。

我看到正确的文件已被提取并最终在大火的输出目录中,但是synth.py无法导入它们:

~/gitrepos/nodejs-vision$ find -L bazel-* -name gcp
bazel-bin/synth.runfiles/nodejs_vision/external/synthtool/synthtool/gcp
bazel-bin/synth.runfiles/synthtool/synthtool/gcp
bazel-nodejs-vision/external/synthtool/synthtool/gcp
bazel-nodejs-vision/bazel-out/k8-fastbuild/bin/synth.runfiles/nodejs_vision/external/synthtool/synthtool/gcp
bazel-nodejs-vision/bazel-out/k8-fastbuild/bin/synth.runfiles/synthtool/synthtool/gcp
bazel-out/k8-fastbuild/bin/synth.runfiles/nodejs_vision/external/synthtool/synthtool/gcp
bazel-out/k8-fastbuild/bin/synth.runfiles/synthtool/synthtool/gcp


这是为什么?我如何解决它?

最佳答案

这看起来像一个实例,其中存储库名称(@synthtool)覆盖了仓库名称(//synthtool)。失败目标的运行文件树看起来像这样:

<runfiles>/synthtool/synthtool/foo.py
     ^          ^        ^       ^
   root         |        |       |
           repo root     |       |
                      package    |
                              modules


Python规则将每个存储库根目录以及运行文件根目录本身添加到PYTHONPATH。 (例如,参见#7067。)这意味着上面的第一条路径片段和第二条路径片段都在您的PYTHONPATH上。您希望相对于repo根解析import synthtool,以便它获取包,但是相对于runfiles根,它解析,因此它尝试导入repo根。

您应该能够通过重命名程序包和/或存储库来减轻这种情况。

关于python - Python无法从bazel git_repository()依赖项导入包,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60085724/

10-11 04:12