首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bazel :testing_example返回ModuleNotFoundError:没有名为“absl.testing”的模块

bazel :testing_example返回ModuleNotFoundError:没有名为“absl.testing”的模块
EN

Stack Overflow用户
提问于 2022-06-25 08:17:56
回答 1查看 350关注 0票数 1

我正在学习Abseil和Bazel,做一些关于单元测试基础的代码我遇到了一个关于a模块的问题:

testing_example.py:

代码语言:javascript
复制
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()

建造:

代码语言:javascript
复制
py_binary(
  name="testing_example",
  deps = ["@io_abseil_py//absl:app"],
  srcs = ["testing_example.py"],
)
代码语言:javascript
复制
$ 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运行时,会得到以下错误:

代码语言:javascript
复制
$ 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?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-28 05:56:12

好吧,我知道了:

我很难找到我需要导入的库(@io_abseil_py//absl/testing:absltest)的路径。以下是更新的构建

代码语言:javascript
复制
py_binary(
  testonly = 1,
  name="testing_example",
  deps = [
    "@io_abseil_py//absl:app", 
    "@io_abseil_py//absl/testing:absltest",
  ],
  srcs = ["testing_example.py"],
) 

解决了。输出:

代码语言:javascript
复制
$ 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72752307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档