Skip to content

Python Processes Spawned From subproccess Module Can't Import Deps #3517

@waltaskew

Description

@waltaskew

🐞 bug report

Affected Rule

py_binary, py_test

Is this a regression?

Yes, this works in 1.6.1

Description

New Python processes from subprocess calls are unable to import their dependencies when run from a bazel run. Something like this:

sv = subprocess.Popen(
    [sys.executable, "-c", "import requests"],
)

fails, even though it's being called from a py_binary which names requests as a dependency. Multiprocessing appears to work correctly, though.

Is there a special incantation to make this work? I've tried various permutations with env=os.environ, cwd=os.getcwd(), shell=True and such to no avail.

The above code works fine when run outside of a bazel run or when bazel run with the 1.6 rules_python release.

🔬 Minimal Reproduction

BUILD:

py_binary(
    name = "subprocess_test",
    srcs = ["subprocess_test.py"],
    visibility = ["//visibility:public"],
    deps = ["@pypi//requests"],
)

subprocess_test.py:

import subprocess
import sys

import requests


def main():
    sv = subprocess.Popen(
        [sys.executable, "-c", "import requests"],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
    )
    out, err = sv.communicate()
    if sv.returncode > 0:
        raise RuntimeError("oh no " + err)


if __name__ == "__main__":
    main()

bazel run subprocess_test

🔥 Exception or Error

RuntimeError: oh no Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

🌍 Your Environment

Operating System:

  
Debian Linux
  

Output of bazel version:

  
bazel 8.3.1
  

Rules_python version:

  
1.7.0-rc4
  

Anything else relevant?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions