Hello, I am learning Pants, and I have an issue ru...
# general
b
Hello, I am learning Pants, and I have an issue running a simple test because it fails to import the module. Here is the repository: https://github.com/pmuller/pants-example-monorepo And this is the error I get:
Copy code
$ ./pants test ::
00:31:26.68 [ERROR] Completed: Run Pytest - packages/libhello/tests/test_hello.py failed (exit code 2).
============================= test session starts ==============================
platform linux -- Python 3.9.14, pytest-7.0.1, pluggy-1.0.0
rootdir: /tmp/pants-sandbox-8eRczK
plugins: cov-3.0.0
collected 0 items / 1 error

==================================== ERRORS ====================================
____________ ERROR collecting packages/libhello/tests/test_hello.py ____________
ImportError while importing test module '/tmp/pants-sandbox-8eRczK/packages/libhello/tests/test_hello.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
packages/libhello/tests/test_hello.py:1: in <module>
    from libhello import hello
E   ModuleNotFoundError: No module named 'libhello'
- generated xml file: /tmp/pants-sandbox-8eRczK/packages.libhello.tests.test_hello.py.xml -
=========================== short test summary info ============================
ERROR packages/libhello/tests/test_hello.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.06s ===============================
What am I getting wrong?
BTW, "./pants dependencies" shows no dependency. I expect pants to guess the dependency given a proper layout. Is that expectation wrong?
e
What does `./pants roots`say? That is of primary importance to get set up correctly when setting up a new repo. It's how pants knows the packages directory is not itself a package.
b
It finds the proper directories:
Copy code
$ ./pants roots
packages/libhello/src
packages/libhello/tests
c
The problem is that with packages/libhello/src as a root,
libhello
isn't a resolvable module, so the test's import isn't resolved
e
And, so, packages/libhello/src/libhello/hello.py is a file in your layout then?
h
In case you haven't seen it yet, this is the general guide for import errors like this https://www.pantsbuild.org/docs/troubleshooting#import-errors-and-missing-dependencies
b
Yes, it's in the layout:
Copy code
$ tree packages/
packages/
└── libhello
    ├── src
    │   ├── BUILD
    │   ├── hello.py
    │   └── __init__.py
    └── tests
        ├── BUILD
        └── test_hello.py
$ cat packages/libhello/src/__init__.py 
from .hello import hello
$ cat packages/libhello/src/hello.py 
def hello(name):
    return f'Hello {name}'
$ cat packages/libhello/tests/test_hello.py 
from libhello import hello


def test_hello():
    assert hello('world') == 'Hello world'
So, you do
from libhello import hello
. There is no `libhello`package, just
hello.py
You probably want to either kill the
src
directory or else just repeat yourself less in the naming.
b
Oh, I see. Let my try to fix it.
e
Put another way, `packages`is currently a bit of a lie. Packages do not lie directly underneath it. Project directories do. The packages do not start until you're under
src
directories.
b
Cool. Now that I understand that subtlety, it works perfectly. Thanks! 😄
❤️ 1
🙌 1
After fighting with Bazel, I have to say: Pants looks awesome!
🔥 1
🔥 1
h
I hope it's a good fit! Please continue to ask any questions and to share feedback, it's super helpful to making the project better 🙂
🙏 1
you're not alone in being tripped up by source roots -- it's probably the most common onboarding issue we see now. I'm trying to think how we can better automate this for new users
h
I once tried to write something to autodetect source roots based on existing imports. It’s a little harder than it might seem at first, but not impossible.
b
I document my sample monorepo publicly on GitHub, hope it'll help others getting started.
🙌 1
w
fwiw: 2.14.x has warnings by default for un-inferred imports, which should help guide debugging
👍 2
(not yet stable, but soonish)