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

User site-packages directory should be ignored #1059

Closed
siddharthab opened this issue Feb 10, 2023 · 12 comments
Closed

User site-packages directory should be ignored #1059

siddharthab opened this issue Feb 10, 2023 · 12 comments
Labels
Can Close? Will close in 30 days if there is no new activity

Comments

@siddharthab
Copy link
Contributor

🚀 feature request

Relevant Rules

py_{library,binary,test}

Description

When running builds and tests, the user site-packages directory is not ignored, which makes the build less hermetic.

Describe the solution you'd like

The registered toolchains could set an env var `PYTHONNOUSERSITE=1` every time they are run. This env var is already set for gazelle runs. More generally, the toolchains could also allow the user to set other environment variables for the toolchain.

Describe alternatives you've considered

I have currently set `--action_env= PYTHONNOUSERSITE=1` to all bazel build steps, but it is less clean because actions that have nothing to do with Python don't need this env var.
@rickeylev
Copy link
Contributor

More generally, the toolchains could also allow the user to set other environment variables for the toolchain.

Can you expand a bit on the rationale/motivation for this idea? It's utility isn't obvious to me, though it's still an interesting idea.

@siddharthab
Copy link
Contributor Author

Can you expand a bit on the rationale/motivation for this idea?

General configurability of the toolchain. For example, this issue itself. Other examples could include PYTHONHASHSEED, PYTHONWARNINGS, PYTHONCOERCECLOCALE, etc.

Obviously, these could also be set for all actions through --action_env, but setting them just for the toolchain is cleaner.

@dflems
Copy link

dflems commented Mar 24, 2023

This one bit me recently. A user had some packages installed in their user site packages dir (~/Library/Python/X.Y/lib/python/site-packages on macOS) and those would always be selected first when importing, no matter what order was specified in the python path. Unfortunately --action_env=PYTHONNOUSERSITE=1 didn't help either in this case.

edit: I can reliably reproduce this with the protobuf package after installing it in the user site packages without installing from wheels: python3 -m pip install --user protobuf --no-binary :all: (using Python 3.9)

@siddharthab
Copy link
Contributor Author

Interesting. For us, the following in our .bazelrc shields us just fine. I verified by deleting some deps from my test target. The tests pass if I don't set PYTHONNOUSERSITE, or if I restore the deps.

build --action_env=PYTHONNOUSERSITE=1

Note that your Python version needs to support the env var.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days.
Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_python!

@github-actions github-actions bot added the Can Close? Will close in 30 days if there is no new activity label Sep 20, 2023
@siddharthab
Copy link
Contributor Author

I think this issue is still relevant.

@github-actions github-actions bot removed the Can Close? Will close in 30 days if there is no new activity label Sep 21, 2023
@alexeagle
Copy link
Collaborator

I think this is already fixed in https://github.com/aspect-build/rules_py because we run the interpreter in isolated mode.

@siddharthab
Copy link
Contributor Author

I was not able to get rules_py to work because of aspect-build/rules_py#159.

@aignas
Copy link
Collaborator

aignas commented Dec 13, 2023

@siddharthab, I think some parts of the isolation are present when running under Python 3.11 or newer. Is your issue related to #382, or is it that you need the hermeticity to be a part of build actions?

@siddharthab
Copy link
Contributor Author

The latter. I want bazel tests to not consider the packages the user has installed in their home directory. The other issue you reference is about PYTHONSAFEPATH env var. This issue is about a different env var.

A general solution to have env vars be part of the toolchain configuration will be helpful in many contexts.

Copy link

This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days.
Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_python!

@github-actions github-actions bot added the Can Close? Will close in 30 days if there is no new activity label Jun 10, 2024
Copy link

This issue was automatically closed because it went 30 days without a reply since it was labeled "Can Close?"

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Can Close? Will close in 30 days if there is no new activity
Projects
None yet
Development

No branches or pull requests

5 participants