Skip to content

fix: explicit PYTHONPATH for isolated test subprocesses (#593)#594

Merged
planetf1 merged 2 commits intogenerative-computing:mainfrom
planetf1:fix/subprocess-pythonpath
Mar 9, 2026
Merged

fix: explicit PYTHONPATH for isolated test subprocesses (#593)#594
planetf1 merged 2 commits intogenerative-computing:mainfrom
planetf1:fix/subprocess-pythonpath

Conversation

@planetf1
Copy link
Contributor

@planetf1 planetf1 commented Mar 6, 2026

Explicit PYTHONPATH for isolated test subprocesses

Type of PR

  • Bug Fix
  • New Feature
  • Documentation
  • Other

Description

Fixes #593

Fixes an issue where tests spawned in isolated subprocess environments (such as via uv run pytest) fail with ModuleNotFoundError. The parent pytest process dynamically injects the package path into sys.path, but this memory state is not naturally inherited by child subprocess.Popen calls. This change explicitly resolves the repository root and injects it into the PYTHONPATH environment variable for both the heavy GPU test runner and the examples runner.

Verified locally, and confirmed working for both HuggingFace and vLLM test suites on LSF HPC nodes.

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code as added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

@github-actions
Copy link
Contributor

github-actions bot commented Mar 6, 2026

The PR description has been updated. Please fill out the template for your PR to be reviewed.

@mergify
Copy link

mergify bot commented Mar 6, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Enforce conventional commit

Wonderful, this rule succeeded.

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert|release)(?:\(.+\))?:

@planetf1 planetf1 changed the title fix: explicit PYTHONPATH for isolated test subprocesses (#592) fix: explicit PYTHONPATH for isolated test subprocesses (#593) Mar 6, 2026
@planetf1 planetf1 force-pushed the fix/subprocess-pythonpath branch from e71015c to 7fadb7d Compare March 6, 2026 09:21
@planetf1 planetf1 marked this pull request as ready for review March 6, 2026 09:21
@planetf1 planetf1 requested a review from a team as a code owner March 6, 2026 09:21

repo_root = str(pathlib.Path(__file__).parent.parent.parent.resolve())
env = os.environ.copy()
env["PYTHONPATH"] = f"{repo_root}{os.pathsep}{env.get('PYTHONPATH', '')}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One concern: Will this cause issues if someone has a PYTHONPATH already set? I'm guessing it would only cause issues if someone has some weird setup. To hedge against that, should we set the variable with PYTHONPATH=env.get('PYTHONPATH', "")<; or :>{repo_root}{os.pathsep}.

At the very least, I don't think it makes sense adding PYTHONPATH to the repo root if already exists. I think that will jumble the path.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can set it to have multiple paths (that's what I was trying to do with my quick example); you set it to the existing path if it exists and then add a link to the base repo as well

@planetf1
Copy link
Contributor Author

planetf1 commented Mar 9, 2026

Thanks @jakelorocco! Ok - I'd originally opted more for isolation (so reverse order) but am ok we go with existing PYTHONPATH configurations as a priority.

I've updated to: existing_path:repo_root (existing first, repo_root as fallback)

This is less invasive and only adds repo_root when the local package isn't already accessible. Changes pushed in commit 4b287b3.

planetf1 added 2 commits March 9, 2026 12:19
Put existing PYTHONPATH first, then append repo_root as fallback.
This is less invasive to user environments while still ensuring
the local package is available when needed.
@planetf1 planetf1 force-pushed the fix/subprocess-pythonpath branch from 4b287b3 to 3748054 Compare March 9, 2026 12:19
Copy link
Contributor

@jakelorocco jakelorocco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm assuming tests pass; and apologies if I made you do extra work; I'm realizing now that I may have misread the initial code given your comment

@planetf1 planetf1 added this pull request to the merge queue Mar 9, 2026
Merged via the queue into generative-computing:main with commit 7bfd18d Mar 9, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Subprocess-based tests (e.g. Examples) fail depending on environment tool due to missing PYTHONPATH

2 participants