question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`bazel run py_binary` loads incorrect module with same name

See original GitHub issue

py_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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
drigzcommented, Jan 16, 2018

@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:

  • for simple modules (with just one file, eg mock), use a genrule to rename mock.py to __init__.py in the repository root, so that Python can find everything in the repo root
  • otherwise, make sure the repo name doesn’t match the module name (eg use @flatbuffers_repo//:runtime_py)

Hope that helps…

0reactions
keithcommented, Apr 1, 2019

I wonder if this is the same issue as https://github.com/bazelbuild/bazel/issues/7754 ?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found