rules_python
                                
                                 rules_python copied to clipboard
                                
                                    rules_python copied to clipboard
                            
                            
                            
                        Getting Started instructions don't work
🐞 bug report
Description
I tried following the instructions in the Getting Started guide in the README [1]. I copied in the first two snippets, with the rules import and the interpreter creation, but when I run the build, I get an error.
I'm guessing this is just slightly outdated based on new releases.
[1] https://github.com/bazelbuild/rules_python
🔬 Minimal Reproduction
Copy the first two snippets from the README (included below) and attempt a build.
rules_python_version = "740825b7f74930c62f44af95c9a4c1bd428d2c53" # Latest @ 2021-06-23
http_archive(
    name = "rules_python",
    sha256 = "3474c5815da4cb003ff22811a36a11894927eda1c2e64bf2dac63e914bfdf30f",
    strip_prefix = "rules_python-{}".format(rules_python_version),
    url = "https://github.com/bazelbuild/rules_python/archive/{}.zip".format(rules_python_version),
)
# -- Interpreter --
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
python_register_toolchains(
    name = "python3_9",
    # Available versions are listed in @rules_python//python:versions.bzl.
    # We recommend using the same version your team is already standardized on.
    python_version = "3.9",
)
load("@python3_9//:defs.bzl", "interpreter")
🔥 Exception or Error
$ bazel run //service
INFO: Repository rules_python instantiated at:
  /Users/jdob/Code/bookstore/WORKSPACE:18:13: in 
Repository rule http_archive defined at:
  /private/var/tmp/_bazel_jdob/300f9ff46946083ee2e5c32c9a7995b7/external/bazel_tools/tools/build_defs/repo/http.bzl:364:31: in 
WARNING: Download from https://github.com/bazelbuild/rules_python/archive/740825b7f74930c62f44af95c9a4c1bd428d2c53.zip failed: class com.google.devtools.build.lib.bazel.repository.downloader.UnrecoverableHttpException Checksum was 09a3c4791c61b62c2cbc5b2cbea4ccc32487b38c7a2cc8f87a794d7a659cc742 but wanted 3474c5815da4cb003ff22811a36a11894927eda1c2e64bf2dac63e914bfdf30f
ERROR: An error occurred during the fetch of repository 'rules_python':
   Traceback (most recent call last):
        File "/private/var/tmp/_bazel_jdob/300f9ff46946083ee2e5c32c9a7995b7/external/bazel_tools/tools/build_defs/repo/http.bzl", line 111, column 45, in _http_archive_impl
                download_info = ctx.download_and_extract(
Error in download_and_extract: java.io.IOException: Error downloading [https://github.com/bazelbuild/rules_python/archive/740825b7f74930c62f44af95c9a4c1bd428d2c53.zip] to /private/var/tmp/_bazel_jdob/300f9ff46946083ee2e5c32c9a7995b7/external/rules_python/temp15994664139200499251/740825b7f74930c62f44af95c9a4c1bd428d2c53.zip: Checksum was 09a3c4791c61b62c2cbc5b2cbea4ccc32487b38c7a2cc8f87a794d7a659cc742 but wanted 3474c5815da4cb003ff22811a36a11894927eda1c2e64bf2dac63e914bfdf30f
ERROR: /Users/jdob/Code/bookstore/WORKSPACE:18:13: fetching http_archive rule //external:rules_python: Traceback (most recent call last):
        File "/private/var/tmp/_bazel_jdob/300f9ff46946083ee2e5c32c9a7995b7/external/bazel_tools/tools/build_defs/repo/http.bzl", line 111, column 45, in _http_archive_impl
                download_info = ctx.download_and_extract(
Error in download_and_extract: java.io.IOException: Error downloading [https://github.com/bazelbuild/rules_python/archive/740825b7f74930c62f44af95c9a4c1bd428d2c53.zip] to /private/var/tmp/_bazel_jdob/300f9ff46946083ee2e5c32c9a7995b7/external/rules_python/temp15994664139200499251/740825b7f74930c62f44af95c9a4c1bd428d2c53.zip: Checksum was 09a3c4791c61b62c2cbc5b2cbea4ccc32487b38c7a2cc8f87a794d7a659cc742 but wanted 3474c5815da4cb003ff22811a36a11894927eda1c2e64bf2dac63e914bfdf30f
ERROR: no such package '@rules_python//python': java.io.IOException: Error downloading [https://github.com/bazelbuild/rules_python/archive/740825b7f74930c62f44af95c9a4c1bd428d2c53.zip] to /private/var/tmp/_bazel_jdob/300f9ff46946083ee2e5c32c9a7995b7/external/rules_python/temp15994664139200499251/740825b7f74930c62f44af95c9a4c1bd428d2c53.zip: Checksum was 09a3c4791c61b62c2cbc5b2cbea4ccc32487b38c7a2cc8f87a794d7a659cc742 but wanted 3474c5815da4cb003ff22811a36a11894927eda1c2e64bf2dac63e914bfdf30f
INFO: Elapsed time: 1.234s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded)
FAILED: Build did NOT complete successfully (0 packages loaded)
  🌍 Your Environment
Operating System:
  
System Version:	macOS 12.2.1 (21D62)
Kernel Version:	Darwin 21.3.0
  
Output of bazel version:
  
Build label: 5.0.0
Build target: bazel-out/darwin-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Wed Jan 19 14:15:55 2022 (1642601755)
Build timestamp: 1642601755
Build timestamp as int: 1642601755
  
                                    
                                    
                                    
                                
Running into a similar issue.
WORKSPACE
http_archive(
    name = "rules_python",
    sha256 = "9fcf91dbcc31fde6d1edb15f117246d912c33c36f44cf681976bd886538deba6",
    strip_prefix = "rules_python-0.8.0",
    url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.8.0.tar.gz",
)
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
load("@rules_python//python:pip.bzl", "pip_install")
python_register_toolchains(
    name = "python3_9",
    python_version = "3.9",
)
load("@python3_9//:defs.bzl", "interpreter")
pip_install(
    name = "my_deps",
    python_interpreter_target = interpreter,
    requirements = "//hypervisor:requirements.txt",
)
BUILD.bazel
py_binary(
    name = "main",
    srcs = ["main.py"],
)
results into
❯ bazel build //hypervisor:main
INFO: Repository python3_9_x86_64-apple-darwin instantiated at:
  /Users/maarten/Development/setup/WORKSPACE:27:27: in <toplevel>
  /private/var/tmp/_bazel_maarten/8c4bfcd606c4d7b95cf418885251b08c/external/rules_python/python/repositories.bzl:254:26: in python_register_toolchains
Repository rule python_repository defined at:
  /private/var/tmp/_bazel_maarten/8c4bfcd606c4d7b95cf418885251b08c/external/rules_python/python/repositories.bzl:164:36: in <toplevel>
INFO: Repository 'python3_9_x86_64-apple-darwin' used the following cache hits instead of downloading the corresponding file.
 * Hash 'fdaf594142446029e314a9beb91f1ac75af866320b50b8b968181e592550cd68' for https://github.com/indygreg/python-build-standalone/releases/download/20220227/cpython-3.9.10+20220227-x86_64-apple-darwin-install_only.tar.gz
If the definition of 'python3_9_x86_64-apple-darwin' was updated, verify that the hashes were also updated.
ERROR: An error occurred during the fetch of repository 'python3_9_x86_64-apple-darwin':
   Traceback (most recent call last):
	File "/private/var/tmp/_bazel_maarten/8c4bfcd606c4d7b95cf418885251b08c/external/rules_python/python/repositories.bzl", line 102, column 17, in _python_repository_impl
		fail(exec_result.stderr)
Error in fail:
ERROR: /Users/maarten/Development/setup/WORKSPACE:27:27: fetching python_repository rule //external:python3_9_x86_64-apple-darwin: Traceback (most recent call last):
	File "/private/var/tmp/_bazel_maarten/8c4bfcd606c4d7b95cf418885251b08c/external/rules_python/python/repositories.bzl", line 102, column 17, in _python_repository_impl
		fail(exec_result.stderr)
Error in fail:
ERROR: /Users/maarten/Development/setup/hypervisor/BUILD.bazel:26:10: //hypervisor:main depends on @python3_9_x86_64-apple-darwin//:python_runtimes in repository @python3_9_x86_64-apple-darwin which failed to fetch. no such package '@python3_9_x86_64-apple-darwin//':
ERROR: Analysis of target '//hypervisor:main' failed; build aborted: Analysis failed
INFO: Elapsed time: 2.643s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)
Operating System:
System Version:	macOS 12.3
Kernel Version:	Darwin 21.4.0
Output of bazel version:
Build label: 5.1.0
Build target: bazel-out/darwin-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Thu Mar 24 14:10:21 2022 (1648131021)
Build timestamp: 1648131021
Build timestamp as int: 1648131021
After some digging it appears that the following line in python/repositories.bzl is the culprit:
        exec_result = rctx.execute(["chmod", "-R", "ugo-w", "lib"])
        if exec_result.return_code:
            fail(exec_result.stderr)
If I change the execute method to use an absolute path for chmod it seems to work:
        exec_result = rctx.execute(["/bin/chmod", "-R", "ugo-w", "lib"])
I have installed the GNU coreutils using Homebrew https://formulae.brew.sh/formula/coreutils which might cause this problem.
Verified that this results in the exit code 1
❯ cd /private/var/tmp/_bazel_maarten/8c4bfcd606c4d7b95cf418885251b08c/external/python3_10_x86_64-apple-darwin
❯ /usr/local/opt/coreutils/libexec/gnubin/chmod -R "ugo-w" lib
❯ echo $?
1
but this works just fine
❯ cd /private/var/tmp/_bazel_maarten/8c4bfcd606c4d7b95cf418885251b08c/external/python3_10_x86_64-apple-darwin
❯ /bin/chmod -R "ugo-w" lib
❯ echo $?
0
Apparently the error comes from the lib/pkgconfig directory, no idea why
❯ chmod -R "ugo-w" lib/pkgconfig; echo $?
1
I've solved my particular issue by updating the PATH in the Bazel action_env:
 bazel build --action_env=PATH=/usr/local/bin/:/usr/local/sbin/:/usr/bin:/bin:/usr/sbin:/sbin //hypervisor:main
Shouldn't this be using which:
https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#which
And print an error message if it cannot find chmod on the path?
Ya, those instructions are not laid out well. I quickly copied code without reading much and got this same problem. After investigating more I finally read that this is only if you want to depend on an unreleased version. I still don't know exactly why the version + hash don't point to the same version (if that's the case) and why that example is front and center and evidently broken, but since I only wanted some "getting started" code sample I just grabbed one of the releases and went with that.