bazel test and virtualenv

2,601 views
Skip to first unread message

tweet...@gmail.com

unread,
Sep 6, 2016, 11:59:48 AM9/6/16
to bazel-discuss
Is there any way to make the bazel test command respect virtualenvs? Right now, if I'm in a virtualenv, I can make a test can pass when I use bazel run, but fail when I run bazel test.

I plan to deploy in a virtualenv within a docker container and it's a pain to not be able to do a bazel test ... before deployment.

Kristina Chodorow

unread,
Sep 6, 2016, 12:42:31 PM9/6/16
to Jacob Huffman, bazel-discuss
What is the failure you're getting? My guess is that either you're not including some runtime depedencies or relying on relative paths that are different between bazel run and bazel-bin/etc.

On Tue, Sep 6, 2016 at 11:59 AM, <tweet...@gmail.com> wrote:
Is there any way to make the bazel test command respect virtualenvs? Right now, if I'm in a virtualenv, I can make a test can pass when I use bazel run, but fail when I run bazel test.

I plan to deploy in a virtualenv within a docker container and it's a pain to not be able to do a bazel test ... before deployment.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/57dab8c6-d8fd-4668-8409-424b77d46bf5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

tweet...@gmail.com

unread,
Sep 6, 2016, 2:16:31 PM9/6/16
to bazel-discuss, tweet...@gmail.com
Say I have a bazel workspace with the following files:

blah.py:
"
import yamlbro
"

py_test.py:
"
import unittest

import blah

class TestStringMethods(unittest.TestCase):

def test_blah(self):
pass

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

BUILD:
"
py_library(
name = "blah",
srcs = [
"blah.py",
],
)

py_test(
name = "py_test",
srcs = [
"py_test.py",
],
deps = [
":blah",
],
)
"

I activate a virtualenv that has yamlbro installed, but my normal python installation does not have it.

bazel run :py_test succeeds.
bazel test :py_test fails with:
"ImportError: No module named yamlbro"

My guess is that whatever config virtualenv does is not making it's way to bazel test.

Kristina Chodorow

unread,
Sep 6, 2016, 2:39:04 PM9/6/16
to Jacob Huffman, bazel-discuss
When Bazel executes tests, it does so in a "sandbox:" it is basically a clean room filesystem with only the things you've declared in your BUILD file in it.  

"bazel run" is not sandboxed.  It just runs the binary on the normal filesystem, so it has access to everything. 

Your options are:
  • The easier way: turn off sandboxing.  Try passing --test_strategy=standalone to bazel.   The downside of doing this is: suppose your test is passing with yamlbro 0.0.1 and then you upgrade virtualenv to use 0.0.2.  Bazel doesn't know about the dependency on yamlbro, so it won't bother rerunning the test (none of the files the test depends on have changed, as far as it knows).  It'll just say the test result is cached and still passing.
  • The more correct way: actually add yamlbro as a runtime dependency for your test.  You could do something like (untested):
new_local_repository(
    name = "yamlbro",
    path = "/path/to/your/yamlbro/lib",
    build_file_contents = """
py_library(
    name = "yamlbro",
    srcs = glob(["**/*.py"]),
    visibility = ["//visibility:public"],
)
""",
)

in your WORKSPACE file, then add a runtime dependency to "@yamlbro//:yamlbro" to your test.  Now, if you update yamlbro, Bazel will notice that a test dep has changed and rerun the test.


--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.

tweet...@gmail.com

unread,
Sep 6, 2016, 8:25:34 PM9/6/16
to bazel-discuss, tweet...@gmail.com
Thanks for the response. I tried the quick and dirty way. I ran the following command:
"bazel test --test_strategy=standalone py_test"

However, that didn't fix the problem. I got the exact same test failure as before.

Kristina Chodorow

unread,
Sep 7, 2016, 10:14:11 AM9/7/16
to Jacob Huffman, bazel-discuss
It looks like you also have to set --spawn_strategy=standalone:

bazel test  --spawn_strategy=standalone --test_strategy=standalone //your-tests

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
Message has been deleted
Message has been deleted

Kristina Chodorow

unread,
Sep 8, 2016, 10:25:18 AM9/8/16
to Jacob Huffman, bazel-discuss
Optimally we'd have a PyPi Skylark repository rule, so you could declare which packages you want to depend on and Bazel would know how to fetch/build them.  However, no such rule exists, yet (AFAIK).


On Wed, Sep 7, 2016 at 12:19 PM, <tweet...@gmail.com> wrote:
On Wednesday, September 7, 2016 at 7:14:11 AM UTC-7, Kristina Chodorow wrote:
> It looks like you also have to set --spawn_strategy=standalone:
>
>
> bazel test  --spawn_strategy=standalone --test_strategy=standalone //your-tests
>
>
> On Tue, Sep 6, 2016 at 8:25 PM,  <tweet...@gmail.com> wrote:
> Thanks for the response. I tried the quick and dirty way. I ran the following command:
>
> "bazel test --test_strategy=standalone py_test"
>
>
>
> However, that didn't fix the problem. I got the exact same test failure as before.
>
>
>
> --
>
> You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
>
> To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
Thanks a lot, that worked.

I might be worth putting that in documentation somewhere. I imagine my use case will be pretty common. It's infeasible (except for large development teams) to build everything you need from PyPi from scratch in Bazel.


--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.

tweet...@gmail.com

unread,
Sep 12, 2016, 11:13:37 AM9/12/16
to bazel-discuss, tweet...@gmail.com
Great! That worked. Thanks a lot. I agree, it's very common to use virtualenvs in modern Python programming and it would be great to see better support.

renn...@gmail.com

unread,
Sep 13, 2017, 8:59:28 PM9/13/17
to bazel-discuss
在 2016年9月12日星期一 UTC-7上午8:13:37,Jacob Huffman写道:
> Great! That worked. Thanks a lot. I agree, it's very common to use virtualenvs in modern Python programming and it would be great to see better support.

I still have the same issue with Bazel 0.4.5. Adding " --spawn_strategy=standalone --test_strategy=standalone" doesn't solve my problem.

ni...@google.com

unread,
Sep 13, 2017, 9:15:30 PM9/13/17
to bazel-discuss
Sorry,
Correct: Bazel 0.5.3 has this problem, while Bazel 0.4.5 doesn't have.

David Stanke

unread,
Sep 14, 2017, 1:45:11 PM9/14/17
to bazel-discuss
Does it occur under Bazel 0.5.4 (latest stable version)?

Ning Ren

unread,
Sep 14, 2017, 6:35:24 PM9/14/17
to David Stanke, bazel-discuss
Bazel 0.5.4 doesn't have this problem.

'David Stanke' via bazel-discuss <bazel-...@googlegroups.com>于2017年9月14日周四 上午10:45写道:
--
You received this message because you are subscribed to a topic in the Google Groups "bazel-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bazel-discuss/oRDudISQXjQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/ea86a59a-2326-47c4-93f8-8f2a63463cf3%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages