'Targeting MINOR python version when building a C++ extension with bazel

I'm building a python C++ extension using Bazel. I periodically switch between virtual environments that use python 3.8 and others that use python 3.9. I've noticed that Bazel seems to get "stuck" on building for 3.9 or 3.8, regardless of which one I'm current in, and I have no idea how it is deciding which to pick.

I'd like to be able to control this, since when bazel builds for the wrong venv I have to bazel clean --expunge to get it on the current venv. Which is a bit extreme.

When I search around I find a lot of answers about choosing between major python versions (2 vs 3), but I need to choose more specifically between python 3.8 and python 3.9.

This is the relevant part of my BUILD file:

cc_binary(
    name = "stim.so",
    srcs = SOURCE_FILES_NO_MAIN + PYBIND_FILES,
    copts = [
        "-O3",
        "-fvisibility=hidden",
        "-march=native",
        "-DSTIM_PYBIND11_MODULE_NAME=stim",
    ],
    includes = ["src/"],
    linkopts = ["-lpthread"],
    linkshared = 1,
    deps = ["@pybind11"],
)

py_wheel(
    name = "stim_dev_wheel",
    distribution = "stim",
    requires = ["numpy"],
    version = "dev",
    deps = [":stim.so"],
)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source