-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Bazel is using python user site packages by default #4939
Comments
this is an expanded version of #890 Also note the effort here: |
I agree we definitely should opt out of user site packages by default. Perhaps we can add an attribute to |
See #7959 for related |
I'm experimenting with using virtual environments in Python 3 to accomplish this, see https://gist.github.com/NathanHowell/5cf4a353a8dd3a1025e682c4707d5bac |
To solve this so far I've started setting |
This fixes the googleapis/gapic-generator#3334 by excluding system-wide site-packages dir from python packages resolution path completely. This pretty much implements the long-standing featrue request for rules_python bazelbuild/bazel#4939, but only in scope of gapic-generator-python.
* fix: Fix namespace packages conflict issue This fixes the googleapis/gapic-generator#3334 by excluding system-wide site-packages dir from python packages resolution path completely. This pretty much implements the long-standing featrue request for rules_python bazelbuild/bazel#4939, but only in scope of gapic-generator-python. * Format with autopep8
In case this helps anyone, you may be able to disable user site packages using env -S in the shebang of the generated py_binary wrapper scripts:
|
Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 14 days unless any other activity occurs or one of the following labels is added: "not stale", "awaiting-bazeler". Please reach out to the triage team ( |
This issue has been automatically closed due to inactivity. If you're still interested in pursuing this, please reach out to the triage team ( |
We tripped over this again today. My user's ~/.local/lib/python3.8/site-packages/ dir caused bazel startup to mysteriously fail (processing WORKSPACE). Details and repros for how to demonstrate how the Python (compiled-in) path functions and how the PYTHONNOUSERSITE=notemptyvalue operates can be found in https://stackoverflow.com/questions/41360022/how-to-ignore-python-module-in-local-lib-python2-7-site-packages. See also bazelbuild/rules_python#2060 which suggests that env variable propagation can be a headache, but I think it's fine in this case. I'm sure somewhere, that someone is abusing the .local and will complain, but I will likely be implementing one of the above workarounds. |
Description of the problem / feature request:
See https://docs.python.org/3/library/site.html#site.ENABLE_USER_SITE for background.
When Bazel runs build actions, we want them to be hermetic by default, maybe with opt-out for certain cases or enforced-through-sandbox for others.
Python by default uses
PYTHONPATH
to find modules, which is easy enough to make hermetic by not including it in the--action_env
-- and indeed, its off by default.Python also has magic to hunt down modules from other places on the filesystem. Some are in
/usr/lib
, some are like/usr/local/lib/python2.7/dist-packages
, etc., but these are par for the course and similar to other languages' rules' level of reliance on/usr
being invariant.However, then we come to Python's "User Site Packages" which are based in
$HOME/.local/lib/...
on Ubuntu, for example. The python interpreter will use these by default, but for Bazel purposes using undeclared files from$HOME
at action runtime seems extremely dangerous, and has caused problems for some of my Bazel users that have a cluttered python environment in their homedir.Feature requests: what underlying problem are you trying to solve with this feature?
Disable Python User Site Packages by default, at least for actions (and also probably repository rules).
Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
What operating system are you running Bazel on?
Ubuntu 16.04.
What's the output of
bazel info release
?release 0.10.1
Any other information, logs, or outputs that you want to share?
See RobotLocomotion/drake#8476 for my project-specific fix -- setting--action_env=PYTHONNOUSERSITE=1
. I haven't tested whether this fixes repository rules, but seems to work for regular build rules, at least.Edit: Setting the action_env only repairs
genrule()s
. Skylarkrule()
s have an empty env, so each one that is calling Python needs to passenv
toactions.run
explicitly.The text was updated successfully, but these errors were encountered: