Skip to content

Conversation

@DolceTriade
Copy link

Currently, rules_pyvenv installs all the python deps by symlinking them in site-packages. However, in bazel, the python deps are actually added to the PYTHONPATH, which means that for some deps, just symlinking into site-packages doesn't work.

However, venv supports this via pth files. pth files will append the directories specified in the pth file to the PYTHONPATH, which exhibits the behavior we want.

Fixes #13

Copy link
Contributor

@jvolkman jvolkman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not seeing this sooner.

pth files were actually my first approach, but I found that PyCharm didn't pick up on changes under the paths pointed to by the pth file, which is why I instead resorted to symlinking everything. If tools worked as expected, pth files would certainly be much more straightforward. That said, it seems like something that we could support optionally, as you are here.

venv.bzl Outdated
for dep in ctx.attr.deps:
if PyInfo not in dep:
continue
print(dep[PyInfo].imports.to_list())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a leftover debug statement.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, whoops!

install_data_file(env_path, file)
else:
install_site_file(site_packages_path, file)
if add_pth:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're creating a pth file, do we still need to call install_site_file? Isn't that redundant?

@DolceTriade
Copy link
Author

Hm, I didn't test using pycharm, I was testing using VSCode. Unfortunately, I don't have access to pycharm.

Currently, rules_pyvenv installs all the python deps by symlinking them in site-packages. However, in bazel, the python deps are actually added to the PYTHONPATH, which means that for some deps, just symlinking into site-packages doesn't work.

However, venv supports this via pth files. pth files will append the
directories specified in the pth file to the PYTHONPATH, which exhibits
the behavior we want.

Fixes cedarai#13
@keith
Copy link

keith commented Aug 22, 2025

I think the base paths should actually be relative to the runfiles of the current venv target instead of the root of the repo. This way any generated files / native extensions are also in that path.

def install_files(env_path: pathlib.Path, files: List[EnvFile], add_pth: bool) -> None:
site_packages_path = find_site_packages(env_path)
pth = site_packages_path / "venv.pth"
pths = set()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we want an ordered set here to be more correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow installing python deps using pth files

3 participants