-
-
Notifications
You must be signed in to change notification settings - Fork 56
Description
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., venv → venv.venv.pth) that was generated in version 1.4.0 to add internal library paths to sys.path.
Affected Versions
- Issue observed:
aspect_rules_py1.6.3 - Working as expected:
aspect_rules_py1.4.0
Error Observed
ModuleNotFoundErrorwhen importing internal libraries in an activatedpy_venvvirtual 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 ModuleNotFoundErrorExpected: 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
ModuleNotFoundErroroccurs (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