Hi! I’m very new to pants, trying it out in an eva...
# general
w
Hi! I’m very new to pants, trying it out in an evaluation. I’m trying to get my pytest to work, and I’ve included a distributable as a dependency, but I can’t get the test to find the module, which is strange. E.g.
Copy code
python_distribution(
    name="dist",
    dependencies=[
      "iai_fl_client:reqs",
      "iai_auth:dist",
      "iai_fl_client/src/iai_fl_client",
      "iai_fl_client/src/iai_fl_client/utils",
      "iai_fl_client/src/iai_fl_client/client",
    ],
    provides=python_artifact(
        name="iai-fl-client",
        version="2.0.24",
    ),
    wheel_config_settings={"--global-option": ["--python-tag", "py37.py38.py39"]},
)
and in the tests BUILD
Copy code
python_tests(
    name="tests",
    timeout=120,
    runtime_package_dependencies=[
      "iai_fl_client:dist",
      "iai_auth:dist",
    ]
)
And when running tests it can’t import
iai_fl_client.utils
- what am I doing wrong?
Copy code
_________________ ERROR collecting tests/test_client_utils.py __________________
ImportError while importing test module '/private/var/folders/xn/yvv0m2ln4dg_vyr8rp8_3xzh0000gn/T/pants-sandbox-dNO8Kw/iai_fl_client/tests/test_client_utils.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/Users/nasron/.pyenv/versions/3.8.13/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
iai_fl_client/tests/test_client_utils.py:3: in <module>
    from iai_fl_client.utils.util import poll_server_info
E   ModuleNotFoundError: No module named 'iai_fl_client.utils'
c
Hi and welcome!
Import issues are usually caused by issues with source roots. Have you gone through this section of the docs? https://www.pantsbuild.org/docs/source-roots
w
So it seems right, because the dependencies are being found (unless thats unrelated).
Copy code
$ ./pants dependencies --transitive iai_fl_client:test | grep iai_fl_client/utils/util.py
iai_fl_client/src/iai_fl_client/utils/util.py

$ ./pants roots | grep iai_fl_client                                       
iai_fl_client/src
What am i missing?
I’ve simplified the build file a lot to
Copy code
python_sources()

python_tests(
    name="test",
    sources=[
       'tests/test*.py'
    ]
)
and relying more on the inferred dependencies
c
Copy code
iai_fl_client/utils/util.py
what is ^^ ? doesn’t seem to be part of any source root? but you’ve filtered your output so hard to tell….
w
Thats the actual module itself that the import error is hitting.
so the structure looks like
Copy code
iai_fl_client\
  src\
     iai_fl_client\
        utils\
  setup.py
and I’m using default root_patterns
and there are multiple projects at the top level
c
not being part of a source root is the root of your issue here I think
w
hmm i’m trying not to move code around but looks like i’ll try that a bit
c
you may also adjust you source roots. if that causes issues you may need to consider reorganizing the structure
w
so - i think the issue is because the source root included in the PEX_EXTRA_SYS_PATH has the module name as a prefix. For example, the __run.sh has
Copy code
export PEX_EXTRA_SYS_PATH=$'iai_fl_client/src ....'
so when the test runs, it tries to import at
./iai_fl_client
instead from
./iai_fl_client/src/iai_fl_client
because the current directory seems to be included in the sys path? So its kind of a conflict in a way. If I manually edit the __run.sh and rename directories in the sandbox, it works.
So for example, if I change it to
Copy code
export PEX_EXTRA_SYS_PATH=$'stuff/iai_fl_client/src ....'
and move the directory under
stuff/
then rerunning
__run.sh
in the sandbox works
So - wondering if others have bumped into this issue? Where the pytest tries to import from the current dir first, instead of precisely what is in the PEX_EXTRA_SYS_PATH list. E.g. https://pantsbuild.slack.com/archives/C046T6T9U/p1676490276561219?thread_ts=1676408544.932079&amp;cid=C046T6T9U
After all that, it seems to work now - I’m not certain what I changed. 🤷