Hi ! I have a noob question about a problem I meet...
# general
t
Hi ! I have a noob question about a problem I meet when I want to run tests. I think it's pretty similar to @stale-nightfall-29801 question. Basically I'm getting this error on Pants version 2.8.0.dev4 :
Copy code
12:41:45.93 [ERROR] Completed: Run Pytest - src/python/classify/tests/test_get_model.py failed (exit code 4).
ImportError while loading conftest '/tmp/process-executiondZwPlt/src/python/classify/tests/conftest.py'.
src/python/classify/tests/conftest.py:17: in <module>
    from tests.utils.email import get_test_email
E   ModuleNotFoundError: No module named 'tests'
I build Classify like this :
Copy code
cat <<EOT >src/python/classify/BUILD
python_sources(
  dependencies=[
     "src/python/classify/main.py",
     "//:python-multipart",
     "//:scikit-learn"
  ]
)

python_tests(
    name="tests",
    sources=["tests/*.py"],
    dependencies=[
      ":classify",
      "//:pg8000"
      ]
      )

python_distribution(
    name="wheel",
    dependencies=[":classify"],
    provides=setup_py(
        name="classify",
        version="0.0.0",
    ),
    wheel=True,
  )
pex_binary(
    name="binary",
    entry_point="__main__.py",
    dependencies=["//:pg8000"],
)
I use this command to run tests :
Copy code
- image: recital/classify
    custom:
      - command: ./pants test src/python/classify/tests:tests
So far I tried everything I thought that could work and I need some help. Thanks !
s
silly question (also a pants noob) ... it there an
Copy code
__init__.py
in your tests directory?
t
Yes 😕
c
I’m not sure how that pants invocation could work, unless you have a BUILD file also in your classify tests folder. I would expect you to run
./pants test src/python/classify:tests
Second, you don’t add your sources to the list of dependencies.. So I would remove the “src/python/classify/main.py” from the dependencies in your
python_sources
.
Then for your error, you include
tests/*.py
in your python tests target, which is not recursive, so it will not have your test sources from utils/ etc.. for that to work, try
sources=["tests/**/*.py"]
might work.
t
Thank you very much for your answers. I'll try that now
c
A suggestion is to have test utils as python_sources, then dependency inference will include them when required.
That is, don’t be afraid to have many targets, it is normal practice to have a BUILD file per directory, rather than recursive path globs for sources.
t
Ok I'll do this too. I thought it was not a good thing to have one in each directory.
c
If you haven’t already, try
./pants tailor
it will help you create targets in your BUILD files for files that are not already part of any target. See https://www.pantsbuild.org/docs/create-initial-build-files
The example repo is also a good place to get a feel for how you might structure a python project using pants: https://github.com/pantsbuild/example-python
e
One thing to keep in mind is that Pants really doesn't care how you lay out your project (or shouldn't). Some layouts will be easier to teach Pants about, but that's all. In this case the error is:
Copy code
src/python/classify/tests/conftest.py:17: in <module>
    from tests.utils.email import get_test_email
E   ModuleNotFoundError: No module named 'tests'
So, then, the question is - what is the
X
in the filesystem path
X/tests/utils/email.py
and is that
X
included in the list printed by
./pants roots
?
👆 1
If
X
is not in the
./pants roots
list, you'll want to (re?) visit: https://www.pantsbuild.org/docs/source-roots#configuring-source-roots to configure your roots so Pants understands your layout.
🙏 1
t
Thank you very much for your detailed answers. I tried @curved-television-6568 solution but I still get the same error message. About @enough-analyst-54434 explanations about roots, I already have src/python/classify set in root parameter. Im going to take a look at the example repo.
h
@tall-truck-67859 If you get stuck, putting the code (or a simplified, redacted version of it) in a github branch would be really helpful in sorting it out :)
🙏 1