rules_python icon indicating copy to clipboard operation
rules_python copied to clipboard

Complete working example using a package from pypi

Open weegreenblobbie opened this issue 1 year ago • 8 comments

🐞 bug report

Description

I'm trying to create a project that uses restructured text and I want to use the rstfmt package. I can install this manually on windows but I thought I'd try to use bazel to make this easier for future users.

I tried following the pip_parse examples but it's not complete. No python interpreter is specified and I can't figure out how to make it work. Can we add a complete, working example? If specific versions of bazel is required please specify as a comment in the example.

🌍 Your Environment

Operating System:

  
Window 10
  

Output of bazel version:

  
bazel version
Bazelisk version: v1.19.0
Build label: 7.0.2
Build target: @@//src/main/java/com/google/devtools/build/lib/bazel:BazelServer
Build time: Thu Jan 25 16:15:43 2024 (1706199343)
Build timestamp: 1706199343
Build timestamp as int: 1706199343
  

Rules_python version:

  
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "rules_python",
    integrity = "sha256-1x0sZ+C86YbhxadzG0aTImhnxFv+C3xeAGciilNvxYA=",
    strip_prefix = "rules_python-0.29.0",
    url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.29.0.tar.gz",
)
  

Anything else relevant?

I'm invoking bazel in a user mode power shell.

weegreenblobbie avatar Feb 01 '24 05:02 weegreenblobbie

What problem are you having? The interpreter is defined here.

jvolkman avatar Feb 08 '24 21:02 jvolkman

I can't get anything to work after hours of trying.

So I've put the pip_parse example into my own repo and tried to do a bare bones setup and it errors out:

https://github.com/weegreenblobbie/example_rules_python/commit/c8c273b0a1fb44046dc79ef5e4aa0275b4a39207

LAUNCHER ERROR: Cannot launch process: "python.exe"

Any help would be appreciated! Nick

On Thu, Feb 8, 2024 at 1:37 PM Jeremy Volkman @.***> wrote:

What problem are you having? The interpreter is defined here https://github.com/bazelbuild/rules_python/blob/main/examples/pip_parse/WORKSPACE#L12-L15 .

— Reply to this email directly, view it on GitHub https://github.com/bazelbuild/rules_python/issues/1741#issuecomment-1934972599, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACPYP32MQAQJ5QKMPX6MN3TYSVARTAVCNFSM6AAAAABCUITYUGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZUHE3TENJZHE . You are receiving this because you authored the thread.Message ID: @.***>

weegreenblobbie avatar Apr 27 '24 20:04 weegreenblobbie

After resetting the contents of requirements*.txt, I get a different error as documented here:

https://github.com/weegreenblobbie/example_rules_python/commit/3b42abf4bbc72614c9daef722f0588465eecd48a

bazel run //tools:main ERROR: no such package '@@pypi//requests': BUILD file not found in directory 'requests' of external repository @https://github.com/pypi. Add a BUILD file to a directory to mark it as a package.

I'm happy to update docs once I understand what I'm doing wrong.

Nick

weegreenblobbie avatar Apr 27 '24 20:04 weegreenblobbie

Here is a working example: https://github.com/pat-jpnk/bazel_pip_example

In your code you need to call if __name__ == "__main__" - you are calling __main__ == "__main__" on accident.

pat-jpnk avatar May 06 '24 09:05 pat-jpnk

Still doesn't work: https://github.com/weegreenblobbie/example_rules_python/commit/d43ac0c910f998364e0a7e491eb5104e8298b9c6

Does bazel + rules_python assume the python interpreter is already installed in the the system's PATH?

weegreenblobbie avatar May 14 '24 02:05 weegreenblobbie

Please reopen.

weegreenblobbie avatar May 14 '24 02:05 weegreenblobbie

Yes, you need a system interpreter for now (#691) and you need to use something like uv pip compile (with extra flags) or the built in rule that is documented in //python:pip.bzl.

Your example from the first glance looks OK, though.

aignas avatar May 15 '24 11:05 aignas

Using @pat-jpnk's examples I'm still failing on Windows after install python 3.11 from python.org:

I updated to python 3.11 in the WORKSPACE file:

PS C:\Users\weegr\code\bazel_pip_example> bazel run //:requirements.update
INFO: Analyzed target //:requirements.update (59 packages loaded, 4254 targets configured).
INFO: Found 1 target...
Target //:requirements.update up-to-date:
  bazel-bin/requirements.update.zip
  bazel-bin/requirements.update.exe
INFO: Elapsed time: 15.082s, Critical Path: 6.31s
INFO: 7 processes: 5 internal, 2 local.
INFO: Build completed successfully, 7 total actions
INFO: Running command line: bazel-bin/requirements.update.exe pip_example/pypi/requirements.in pip_example/pypi/requirements_lock.txt //:requirements.update '--resolver=backtracking' --allow-unsafe --generate-hashes
Updating pypi/requirements_lock.txt
WARNING: --strip-extras is becoming the default in version 8.0.0. To silence this warning, either use --strip-extras to opt into the new default or use --no-strip-extras to retain the existing behavior.
Error: Could not open file 'pypi/requirements_lock.txt': No such file or directory

weegreenblobbie avatar Jun 04 '24 17:06 weegreenblobbie