Bazel Run:testing_example返回ModulenotFoundError:无模块名为' absl.testing'

发布于 2025-02-10 13:58:42 字数 2267 浏览 0 评论 0原文

我正在研究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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隱形的亼 2025-02-17 13:58:42

好的,我明白了:

我很难找到需要导入的库的路径(@io_abseil_py // absl/testing:absltest)。
这是已

py_binary(
  testonly = 1,
  name="testing_example",
  deps = [
    "@io_abseil_py//absl:app", 
    "@io_abseil_py//absl/testing:absltest",
  ],
  srcs = ["testing_example.py"],
) 

解决的更新构建。输出:

$ 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.236s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Running tests under Python 3.9.7: /Users/maion/opt/anaconda3/bin/python3
[ 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

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

py_binary(
  testonly = 1,
  name="testing_example",
  deps = [
    "@io_abseil_py//absl:app", 
    "@io_abseil_py//absl/testing:absltest",
  ],
  srcs = ["testing_example.py"],
) 

Solved. Output:

$ 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.236s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Running tests under Python 3.9.7: /Users/maion/opt/anaconda3/bin/python3
[ 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文