Hello everyone! So I’ve finally gotten to the poi...
# general
l
Hello everyone! So I’ve finally gotten to the point where I gave up last time. I have a django project with the tests inside of the each of the django apps, and the configs/settings at the top of the repo. When I run the tests, I get the following error:
Copy code
ImportError: No module named 'config.settings'

pytest-django could not find a Django project (no manage.py file could be found). You must explicitly add your Django project to the Python path to have it picked up.
Here is the repo in question: https://github.com/xuru/monolith-to-monorepo/tree/pants_baseline If anyone could give me some pointers on how to make this work, I would much appreciate it!
1
r
Hey did you try looking at pants own example of how to integrate Django? https://github.com/pantsbuild/example-django
l
Yep, I did. Got most of the configuration from there. However, I can’t move around a lot of the code to make the tool work, so I got to get the tool to work with the way the code is structured, and eventually we will organized things better
r
l
huh, I missed this when I searched. Let me see if this will work. Thing is, I can run pytest by it’s self it figures out the PYTHONPATH, so there must be a way to configure it without changing code. I’ll see if this works though
r
django is so much magic!
l
haha, tell me about it
r
Sorry I am bit having hard time finding where all your tests are in this repo?
l
Copy code
✕ mtom/api/tests/pagination/test_get_paginated_response.py:../../../tests failed in 4.12s.
✕ mtom/authentication/tests/apis/test_user_login.py:../../../tests failed in 4.36s.
✕ mtom/common/tests/models/test_random_model.py:../../../tests failed in 4.13s.
✕ mtom/common/tests/services/test_model_update.py:../../../tests failed in 4.48s.
✕ mtom/common/tests/utils/test_inline_serializer.py:../../../tests failed in 4.50s.
✕ mtom/files/tests/flows/test_direct_upload.py:../../../tests failed in 4.48s.
✕ mtom/files/tests/flows/test_standard_upload.py:../../../tests failed in 4.50s.
✕ mtom/testing_examples/tests/selectors/test_school_list_school_courses.py:../../../tests failed in 4.46s.
✕ mtom/testing_examples/tests/services/test_roster_create.py:../../../tests failed in 4.48s.
✕ mtom/testing_examples/tests/services/test_roster_validate_period.py:../../../tests failed in 4.12s.
✕ mtom/testing_examples/tests/services/test_student_create.py:../../../tests failed in 4.36s.
✕ mtom/users/tests/services/test_user_create.py:../../../tests failed in 4.10s.
If that helps
👍 1
r
Yes thank you! And where have you defined the
python_tests
target for this? I don’t see it inside these directories
l
mtom/BUILD
r
And
python_sources
for
motm
has no glob like pattern you use for
python_tests
inside
dependencies
?
l
Unfortunately that change didn’t change anything
😞 1
r
python_sources
only globs by default the
.py
files inside current directory but nothing beyond that
l
So, if I use only one BUILD file at that level it doesn’t recursively find python files? So I should add sources like I do in
python_tests
?
ah, ok
r
yes
either add BUILD with
python_sources
in every directory or you need to glob it. But ensure that only non test files are part of
python_sources
. By the way you need use
sources
for that, not dependencies
l
By having a BUILD file and one python_sources target, if code changes anywhere, the whole thing would need to be rebuilt/tested? Which is why they suggest using tailor and generate them in each directory?
r
I don’t think that’s the reason though. Pants is smart enough to not rebuild anything which doesn’t depend on that file. There are other benefits of BUILD file for every directory like you can have ignore something at directory level e.g. skip options like
skip_mypy
etc.
l
Yeah, I kind of figured I would work backwards, and put them in where I needed the exceptions.
r
Although now even pants doesn’t care very much about it. If you search slack you will see there is no such strong recommendation.
By the way when providing glob, you should ensure you are only matching python source modules and not tests stuff. So you will have to add some ignore pattern like
!test_*.py
inside the python sources. Of course this would have to cover all the tests file in current and directories below Check the docs for example https://www.pantsbuild.org/docs/reference-python_sources#codesourcescode Pants will otherwise complain that there are multiple owners.
🙌 1
l
ok, after I rebuilt the lockfiles, and ran test, it gave me the same error
Copy code
python_sources(
    dependencies=[
        "//:req",
        "config:config",  # For settings.py.
    ],
    sources=['*.py', '**/*.py', '!test_*.py', '!conftest.py']
)
r
I think you need to do the same thing with
config
BUILD file too.
l
doh, right
Sweet, I think that did it.
r
by the way you need to ignore all test files in current and sub directories. So I would write
Copy code
sources=["**/*.py", "!**/test_*.py", "!**/conftest.py"]
🙌 1
Just ensure that these patterns are correct. I also google these things about glob pattern
You don’t need to add
//:req
dependencies. Pants will figure out all this stuff on its own as long as you have
poetry_requirements
when using poetry
l
Thank you very much for that, i was wondering
r
Even
config:config
isn’t required. Most of the time pants figures out all such dependencies based on your import statement.
l
Now that I got it working, I’ll play around with it. Thank you
👍 1