<#18178 `pants test` fails with `ModuleNotFoundErr...
# github-notifications
q
#18178 `pants test` fails with `ModuleNotFoundError` with conditional installation in requirements.txt Issue created by MichalOleszak Describe the bug I have the following project structure:
Copy code
project
  - pants.toml
  - module_A
      - requirements.txt
      - src/script_A.py   # imports tensorflow 
      - tests
          - test_A.py   # imports a function from script_A.py
          - BUILD
      - BUILD
  - utils
       - requirements.txt
       - src/utils.py
       - tests
          - test_utils.py
          - BUILD
       - BUILD
module_A/requirements.txt
sources all the requirements from utils and also adds tensorflow, which is installed in its regular version on Linux and MacOS with Intel chip, and in ints special build for Macs with the M1 chip:
Copy code
-r ../utils/requirements.txt
tensorflow==2.9.1; sys_platform == "linux" or platform_machine == "x86_64"
tensorflow-macos==2.9.1; platform_machine == "arm64"
module_A/BUILD
specifies the python requirements:
Copy code
python_requirements(
    name="reqs", 
    source="requirements.txt",
)
and
module_A/tests/BUILD
specifies the test to be run:
Copy code
python_sources(name="module-a-tests", sources=["test_*.py"])
python_tests(name="test-module-a")
I am using the outermost directory (
project
) as the source root. When I run
pants test ::
, it fails with
ModuleNotFoundError: No module named 'tensorflow'
. If I comment-out the line
tensorflow==2.9.1; sys_platform == "linux" or platform_machine == "x86_64"
from
requirements.txt
and only install the MacOS version, than it works. But I need to have both these line there so that it also works for teammates using linux and MacOS with the Intel chip. I am also able to make it work by setting
python_sources(dependencies=["module_A:reqs#tensorflow-macos"])
in its BUILD file but this still forces me to manually set which package build I want to use. I expected to be able to solve the problem by adding
module_mapping
to
module_A/BUILD
(since both
tensorflow
and
tensorflow-macos
are importable as
tensorflow
) but I could not make it work. Can you please tell me what I am doing wrong? Pants version 2.14.1 OS MacOS 12.5.1 with M1 chip pantsbuild/pants