Skip to content

ModuleNotFoundError when importing internal libraries with py_venv in aspect_rules_py 1.6.3 #696

@MohitBhar

Description

@MohitBhar

Summary

Importing internal libraries in py_venv virtual environments fails with ModuleNotFoundError in aspect_rules_py 1.6.3. The issue is caused by the missing {target_name}.venv.pth file (e.g., venvvenv.venv.pth) that was generated in version 1.4.0 to add internal library paths to sys.path.

Affected Versions

  • Issue observed: aspect_rules_py 1.6.3
  • Working as expected: aspect_rules_py 1.4.0

Error Observed

  • ModuleNotFoundError when importing internal libraries in an activated py_venv virtual environment:
  • Virtual environment name is not displayed in the terminal prompt (should show (.venv.venv) ➜)

Root Cause

The ModuleNotFoundError occurs because the {target_name}.venv.pth file does not appear to be generated in version 1.6.3. Based on the behavior in version 1.4.0, this file seems to be necessary to add internal library paths to sys.path when the virtual environment is activated.

Steps to Reproduce

A minimal reproduction case is available at: https://github.com/MohitBhar/py-venv-pth-issue/tree/main

git clone https://github.com/MohitBhar/py-venv-pth-issue/tree/main
cd py-venv-pth-issue
bazel run //app:venv
source .app+venv/bin/activate 
python app/main.py  # Fails with ModuleNotFoundError

Expected: App runs successfully; prompt shows (.venv.venv) ➜
Actual: ModuleNotFoundError: No module named 'calculator'

Regression Test

The repository includes a regression test:

bazel test //app:venv_test --test_output=all
  • Test PASSES when imports work (no regression)
  • Test FAILS when ModuleNotFoundError occurs (regression detected)

Verification (1.4.0 works)

Change MODULE.bazel:

bazel_dep(name = "aspect_rules_py", version = "1.4.0")

Then run:

rm -rf .app+venv
bazel run //app:venv
source .app+venv/bin/activate
python app/main.py  # ✅ Works!

Environment

  • Bazel version: 8.4.2
  • Python version: 3.9
  • OS: macOS

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions