Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python rules are not hermetic #890

Closed
johnynek opened this issue Feb 13, 2016 · 4 comments
Closed

Python rules are not hermetic #890

johnynek opened this issue Feb 13, 2016 · 4 comments
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-Python Native rules for Python type: feature request
Milestone

Comments

@johnynek
Copy link
Member

the code is run by the system python, it seems, so it depends on what is installed.

It would be nice to only require bazel.

@damienmg damienmg added type: feature request P2 We'll consider working on this in future. (Assignee optional) labels Feb 15, 2016
@ulfjack ulfjack added P3 We're not considering working on this, but happy to review a PR. (No assignee) and removed P2 We'll consider working on this in future. (Assignee optional) labels Jun 15, 2016
@ulfjack ulfjack added this to the 1.0 milestone Jun 15, 2016
@drigz
Copy link
Contributor

drigz commented Jun 19, 2018

I found a particularly difficult effect of this. If the environment has google-auth (or possibly other packages that use setuptools's namespaces) installed as a system or user site- or dist-package, then Python 2.7 will load a .pth file that setups up the google namespace incompatibly with Bazel. In particular, google.__path__ = ['/path/to/site-packages/google'], but not the google directories in the py_binary's runfiles tree. This results in an error like:

Traceback (most recent call last):
  File "test.py", line 31, in <module>
    from google.cloud import pubsub
ImportError: No module named cloud

The only workaround I could find is to delete either uninstall the site package or delete the .pth file. I believe it is also possibly to work around this by activating an empty virtualenv before invoking Bazel, but I haven't tried.

@brandjon brandjon added team-Rules-Python Native rules for Python and removed category: rules > python labels Oct 18, 2018
@brandjon
Copy link
Member

I've heard about issues with the google namespace package before. Not sure where the canonical thread for that is.

About hermetic python: In order to not rely on the system python interpreter, the interpreter would have to somehow be part of the workspace, either vendored into the main repo or else referred to as an external repo from the WORKSPACE file. The former option would be undertaken by the user. I suppose the latter option would imply that we either upstream BUILD files into CPython, or else maintain a canonical Python-in-Bazel repo somewhere.

@drigz
Copy link
Contributor

drigz commented Oct 19, 2018

Re: namespace packages: bazelbuild/rules_python#14 but if I remember correctly, that's about a different implementation of namespace packages to the .pth files mentioned above.

@brandjon
Copy link
Member

This thread discusses two separate issues that can be dupped against other bugs, so I'll go ahead and close it.

For the hermeticity issue: It's standard practice that Bazel can invoke either system tools (non-hermetically) or tools vendored into the workspace. Follow #7375 for the feature work needed to give better control over what runtime the Python rules use.

For the imports issue: Name clashes happen in Python, and I don't think Bazel is in a position to avoid them when two separate projects insist on being called the same thing. But it sounds like the issue here is that a system library is conflicting with the name of a top-level package //google in some repo. There are two points to note: First, when conflicts happen between system libraries and user libraries, the best practice seems to be to resolve in favor of the system; see #5899. Second, top-level packages are currently directly importable because repo roots are put on the PYTHONPATH, but this is an anti-pattern and will be prohibited with #7067. Taken together, these two fixes should mean that you no longer have clashes between system libs and Bazel-built libs, provided that no one names their repo the same thing as a Python lib, and that no one adds a conflicting name to a py_library's imports.

cloud-robotics-github-robot pushed a commit to googlecloudrobotics/core that referenced this issue Mar 26, 2019
Having a locally installed google-auth Python package may cause bazel
test to fail, a consequence of bazel's Python rules not being hermetic.

Users following our how-to guides may run into this, as they are asked
to pip install google-auth.

See also:
bazelbuild/bazel#890 (comment)

Change-Id: Idc0d34ecc81b910b663bf380da14c3e7cf6d8415
GitOrigin-RevId: bec997c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-Python Native rules for Python type: feature request
Projects
None yet
Development

No branches or pull requests

6 participants