Skip to content

Instantly share code, notes, and snippets.

@luciferous
Created June 25, 2016 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luciferous/1ed1defdb497865ed233e6796b615121 to your computer and use it in GitHub Desktop.
Save luciferous/1ed1defdb497865ed233e6796b615121 to your computer and use it in GitHub Desktop.
Bazel: Python modules

Why does Bazel build the Python module under app? Is there a parameter to make it build foo so that I can write from foo import bar? (Note: if I change foo_test.py to use from app.foo import bar the command below succeeds.)

This is my directory structure.

$ find .
.
./app
./app/foo
./app/foo/__init__.py
./app/foo/BUILD
./app/foo/tests
./app/foo/tests/foo_test.py
./WORKSPACE

These are the contents of my files.

$ find app -type f -print -exec cat {} \; -exec echo \;
app/foo/__init__.py
def bar():
    return 'hello'

app/foo/BUILD
py_library(name = "foo", srcs = glob(["**/*.py"]))
py_test(name = "foo_test", srcs = ["tests/foo_test.py"], deps = [":foo"])

app/foo/tests/foo_test.py
import unittest
from app.foo import bar

class FooTests(unittest.TestCase):
    def test_bar(self):
        assert bar() == 'hello'

if __name__ == '__main__':
    unittest.main()

This is the error.

$ bazel test app/foo:foo_test --verbose_failures --test_output=all
INFO: Found 1 test target...
FAIL: //app/foo:foo_test (...).
INFO: From Testing //app/foo:foo_test:
==================== Test output for //app/foo:foo_test:
Traceback (most recent call last):
  File "/private/var/tmp/.../tmp/bazel-out/local-fastbuild/bin/app/foo/foo_test.runfiles/__main__/app/foo/tests/foo_test.py", line 2, in <module>
    from foo import bar
ImportError: No module named foo
================================================================================
Target //app/foo:foo_test up-to-date:
  bazel-bin/app/foo/foo_test
INFO: Elapsed time: 0.145s, Critical Path: 0.06s
//app/foo:foo_test                                                       FAILED in 0.1s
  /private/var/tmp/.../tmp/bazel-out/local-fastbuild/testlogs/app/foo/foo_test/test.log

Executed 1 out of 1 tests: 1 fails locally.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment