Skip to content

Smooth IDE support for python_rules #1401

Open
@RmStorm

Description

@RmStorm

🚀 feature request

Description

I would like smooth editor support of the python_rules. Now when I open an editor (tried helix and vscode) neither recognizes any of the third party imports properly breaking autocomplete and type checking.

Describe the solution you'd like

Something like what the rules_go people have done but I imagine that pyright (the most commonly used python language server) does not have support for something similar to the gopackagedriver.. So I was thinking more along the lines of a shell script that manipulates PATH and PYTHONPATH to achieve something similar. still hacky but better than what I have now. It's also something that would work for other python language server's such as ruff-lsp.

Describe alternatives you've considered

Manually make a venv with the same dependencies and activate that before starting my editor. Works fine but it's hackish and it won't work for generated code. For example python files generated from protobuf by Bazel..

Activity

aignas

aignas commented on Sep 21, 2023

@aignas
Collaborator

Note, that pyright is a static type checker https://github.com/microsoft/pyright. Pylense is a closed-source LSP that is only available within VSCode.

For now you could use https://github.com/cedarai/rules_pyvenv to create a virtual environment for a target, that provides good editor integration. That may work with generated code.

alexeagle

alexeagle commented on Oct 2, 2023

@alexeagle
Contributor

https://github.com/aspect-build/rules_py has the explicit goal of providing excellent Python IDE support for py_* rules.

github-actions

github-actions commented on Mar 30, 2024

@github-actions

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!

added
type: pippip/pypi integration
and removed
Can Close?Will close in 30 days if there is no new activity
on Mar 31, 2024
added a commit that references this issue on Jun 6, 2024
github-actions

github-actions commented on Sep 27, 2024

@github-actions

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!

tpasternak

tpasternak commented on Sep 28, 2024

@tpasternak

Recently, the IntelliJ/PyCharm plugin has received a lot of great patches, I think your could try it

antspy

antspy commented on Oct 1, 2024

@antspy

https://github.com/aspect-build/rules_py has the explicit goal of providing excellent Python IDE support for py_* rules.

Hi,

Would it be possible to provide an example on how to enable the IDE support? I can't seem to find any specific explanation of how to integrate these rules with any IDE (e.g. vscode).

Thanks!

alexeagle

alexeagle commented on Oct 1, 2024

@alexeagle
Contributor

@antspy
bazel run //my_target.venv puts the virtualenv folder in your source tree, then you point your editor at it, for example https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment

In my testing, it was a lot easier to get this to work when the virtualenv is in the repo root, for some reason it was hard to get VSCode to discover one that's in a subfolder - but according to the docs it should work. LMK what you find.

antspy

antspy commented on Oct 1, 2024

@antspy

@alexeagle
Thank a lot for your answer - this worked!

For posterity:
I have created a py_binary in the root BUILD.bazel file, which contains all third-party dependencies. Then I run bazel run mybinary.venv, which creates a .venv folder in the root directory.

I point VSCode to this environment (it should recognize it automatically) and things seem to be working! :)

For the future:

  1. Make the fake binary depend on the requirements automatically, so I don't have to specify external dependencies twice. I am sure they can be extracted somehow from the pip_compile rule, but I have not investigated this.
  2. Make the process less manual (e.g. having the bazel vscode extension automatically build these things)

Anyway, thank you for the help! :)

alexeagle

alexeagle commented on Oct 2, 2024

@alexeagle
Contributor
antspy

antspy commented on Oct 3, 2024

@antspy

That's so cool, thanks a lot for doing this!! Very much appreciated :) :)

added theissue type on May 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @alexeagle@aignas@antspy@tpasternak@RmStorm

        Issue actions

          Smooth IDE support for python_rules · Issue #1401 · bazel-contrib/rules_python