`bazel run py_binary` loads incorrect module with same name
See original GitHub issuepy_binary can end up importing a file from the source tree that it doesn’t depend on. It can also get confused by stale .pyc files that Python writes into the source tree (is that expected behaviour?), even once this file has been removed.
My example may seem contrived, but I ran into this problem when trying to port the rosmaster script to Python - since Bazel rejects Python files without the .py
extension, I renamed it to rosmaster.py
. This caused it to import itself and fail. After I renamed it to avoid this, I had to manually delete the .pyc file before bazel run
would work.
Repro
> echo > WORKSPACE
> cat lib/hello.py
def hello():
print("hello, world!")
> cat bin/hello2.py
import hello
if __name__ == "__main__":
print "loaded hello from", hello.__file__
hello.hello()
> cp bin/hello2.py bin/hello.py
> cat BUILD.bazel
py_library(
name = "hello_lib",
srcs = ["lib/hello.py"],
imports = ["lib"],
)
py_binary(
name = "hello2",
srcs = ["bin/hello2.py"],
deps = [":hello_lib"],
)
> bazel run :hello2
[SNIP: build output]
INFO: Running command line: bazel-bin/hello2
loaded hello from /usr/local/google/home/rodrigoq/git/bazeltest/stale_pyc/bin/hello.py
Traceback (most recent call last):
File "/usr/local/google/home/rodrigoq/.cache/bazel/_bazel_rodrigoq/64c22e296c4c5490bc13ab2111abf684/execroot/__main__/bazel-out/local-fastbuild/bin/hello2.runfiles/__main__/bin/hello2.py", line 5, in <module>
hello.hello()
TypeError: 'module' object is not callable
ERROR: Non-zero return code '1' from command: Process exited with status 1
> rm bin/hello.py
> bazel run :hello2
[SNIP: build output]
INFO: Running command line: bazel-bin/hello2
loaded hello from /usr/local/google/home/rodrigoq/git/bazeltest/stale_pyc/bin/hello.pyc
Traceback (most recent call last):
File "/usr/local/google/home/rodrigoq/.cache/bazel/_bazel_rodrigoq/64c22e296c4c5490bc13ab2111abf684/execroot/__main__/bazel-out/local-fastbuild/bin/hello2.runfiles/__main__/bin/hello2.py", line 5, in <module>
hello.hello()
TypeError: 'module' object is not callable
ERROR: Non-zero return code '1' from command: Process exited with status 1
> rm bin/hello.pyc
> bazel run :hello2
[SNIP: build output]
INFO: Running command line: bazel-bin/hello2
loaded hello from /usr/local/google/home/rodrigoq/.cache/bazel/_bazel_rodrigoq/64c22e296c4c5490bc13ab2111abf684/execroot/__main__/bazel-out/local-fastbuild/bin/hello2.runfiles/__main__/lib/hello.pyc
hello, world!
Environment info
- Operating System: Ubuntu 14.04
- Bazel version: 0.7.0
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Python Rules | Bazel
If you want to run a py_binary from within another binary or test (for example ... If main is unspecified, this should be...
Read more >How to use Bazel's py_library imports argument - Stack Overflow
So when python evaluates import boto3 , it sees bazel-out/local-fastbuild/bin/example.runfiles/boto3/__init__.py from the first entry and uses ...
Read more >README.md - Aspect's Bazel Documentation
This repository is the home of the core Python rules -- py_library , py_binary , py_test , and related symbols that provide the...
Read more >Experimentations on Bazel: Python (1), FastAPI
If you launch with bazel run //exp_python/webapp:run -- --reload and do change into main.py they should be detected and apply.
Read more >Bazel Tutorial - Running a python script and installing PIP ...
In this video I go through the steps required to use the basic python rules in bazel to:- Download a standalone (hermetic) python...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@scottcjt If I’m understanding right, I’ve run into that issue too (Python confusing the external repo with the module directory). We use one of two workarounds:
mock
), use a genrule to renamemock.py
to__init__.py
in the repository root, so that Python can find everything in the repo root@flatbuffers_repo//:runtime_py
)Hope that helps…
I wonder if this is the same issue as https://github.com/bazelbuild/bazel/issues/7754 ?