Bazel Run:testing_example返回ModulenotFoundError:无模块名为' absl.testing'
我正在研究Abseil和Bazel,做一些有关我遇到了关于absltest模块的问题:
testing_example.py:
from absl import app
from absl import flags
from absl.testing import absltest
FLAGS = flags.FLAGS
class TestStringMethods(absltest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
absltest.main()
build:build:
py_binary(
name="testing_example",
deps = ["@io_abseil_py//absl:app"],
srcs = ["testing_example.py"],
)
$ python testing_example.py
Running tests under Python 3.10.5: /Users/maion/opt/anaconda3/envs/abslPy/bin/python
[ RUN ] TestStringMethods.test_isupper
[ OK ] TestStringMethods.test_isupper
[ RUN ] TestStringMethods.test_split
[ OK ] TestStringMethods.test_split
[ RUN ] TestStringMethods.test_upper
[ OK ] TestStringMethods.test_upper
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
但是当我使用Bazel运行时,我会收到以下错误:
$ bazel run //example_py:testing_example
(...)
INFO: Analyzed target //example_py:testing_example (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //example_py:testing_example up-to-date:
bazel-bin/example_py/testing_example
INFO: Elapsed time: 0.108s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Traceback (most recent call last):
File "/private/var/tmp/_bazel_maion/29bdcd3c09c56f1b80d3a22707ccfd7e/execroot/__main__/bazel-out/darwin_arm64-fastbuild/bin/example_py/testing_example.runfiles/__main__/example_py/testing_example.py", line 3, in <module>
from absl.testing import absltest
ModuleNotFoundError: No module named 'absl.testing'
为什么它找不到模块absl.testing?
I'm studying Abseil and Bazel, doing some code regarding Unit Tests Basics I got a problem regarding the absltest module:
testing_example.py:
from absl import app
from absl import flags
from absl.testing import absltest
FLAGS = flags.FLAGS
class TestStringMethods(absltest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
absltest.main()
BUILD:
py_binary(
name="testing_example",
deps = ["@io_abseil_py//absl:app"],
srcs = ["testing_example.py"],
)
$ python testing_example.py
Running tests under Python 3.10.5: /Users/maion/opt/anaconda3/envs/abslPy/bin/python
[ RUN ] TestStringMethods.test_isupper
[ OK ] TestStringMethods.test_isupper
[ RUN ] TestStringMethods.test_split
[ OK ] TestStringMethods.test_split
[ RUN ] TestStringMethods.test_upper
[ OK ] TestStringMethods.test_upper
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
But when I run with bazel I get the following error:
$ bazel run //example_py:testing_example
(...)
INFO: Analyzed target //example_py:testing_example (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //example_py:testing_example up-to-date:
bazel-bin/example_py/testing_example
INFO: Elapsed time: 0.108s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Traceback (most recent call last):
File "/private/var/tmp/_bazel_maion/29bdcd3c09c56f1b80d3a22707ccfd7e/execroot/__main__/bazel-out/darwin_arm64-fastbuild/bin/example_py/testing_example.runfiles/__main__/example_py/testing_example.py", line 3, in <module>
from absl.testing import absltest
ModuleNotFoundError: No module named 'absl.testing'
Why it is not finding the module absl.testing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我明白了:
我很难找到需要导入的库的路径(
@io_abseil_py // absl/testing:absltest
)。这是已
解决的更新构建。输出:
Ok, I got it:
I was having trouble finding the path to the library I needed to import (
@io_abseil_py//absl/testing:absltest
).Here is the updated BUILD
Solved. Output: