Search code examples
pythonbuildstdinbazel

Python raw_input & Bazel: EOFError: EOF when reading a line


I am attempting to use Bazel as the build system for a python program. I'll use an example of test.py. My BUILD file includes:

package(default_visibility = ["//visibility:public"])
py_binary(
    name = "test",
    srcs = [
        "test.py",
    ],
    main = "test.py",
    deps = []
)

My test.py includes:

name = raw_input("Please enter your name\n")

print "your name is {}".format(name)

When I run though normal compiler I get:

Please enter your name
python
your name is python

However, when I run as bazel run :test I get:

Traceback (most recent call last):
  File "[directory]test.[directory]python_test.py", line 1, in <module>
    name = raw_input("Please enter your name\n")
EOFError: EOF when reading a line
Please enter your name

Solution

  • Which version of bazel are you using? In older versions of bazel, the bazel client doesn't connect standard in to the binary when run with bazel run. In 0.12 and up you can pass --direct_run to connect standard in. In 0.15 this became the default and --direct_run is a noop.

    You can also run the resulting binary itself (in your example if the build file is at the root of the workspace it would be bazel-bin/test)