Is there a way I can just declare a whole python d...
# general
f
Is there a way I can just declare a whole python directory/module, including its submodules as a dependency? Pants does not seem very good at the dependency inferring with our Django projects, and I don't want to have to list out every single submodule as a dependency to get an app to run
w
Like, as a glob?
Copy code
python_sources(
    name="lib",
    sources=["**/*.py"],
)
b
As SJ says, I think if you define a target with a glob then
dependencies=[":lib"]
will depend on all of those files There's not a way to glob over targets, where there's separate
python_sources()
targets and one can write
dependencies=["path/to/**:*]
(or some other hypothetical syntax). (BTW, we're happy to help you get the dependency inference working more smoothly if you'd like, but no obligation of course!)
f
That glob isn't working unfortunately. I keep getting errors along the lines of
DifferingFamiliesError: Expected AddressMaps to share the same parent directory
It seems like the only way forward is to manually specify a bunch of dependencies, which isn't ideal at all. I see that the example Django project already does this for migrations. But we'll also need to do it for URL patterns, and probably for the various
apps.get_model()
calls as well. There are tons of those in our code base which is why I'd prefer it (at least for now) if we could just avoid the clever clogs automated inference and just tell Pants that this code depends on that-module, the whole-module-please-don't-leave-anything-out
Or if there's some way I can tell Pants that
apps.get_model('x', 'y')
translates to
from x import y
that might work?
Musing on this I'm wondering if anyone else has used Pants in anger for Django before? I'd be interested in seeing a real example of it if one exists out there, because the example given in the docs is limited to the point of uselessness, if I'm being honest.
w
Re: uselessness - you're referring to this example? https://github.com/pantsbuild/example-django
Are you able to share the structure of your repo? Or an anonymized reproduction? There are a lot of ways to structure stuff in Pants to escape hatch - but it's hard (for me anyways) to tell what's going on right now
f
I've got several Django apps currently living under
src/
, i.e.
src/app1
src/app2
etc. Some of these are split futher like
src/app1/core
,
src/app1/admin
I've then got a separate dir atm for web services like
service/web_admin
. These contain my settings files with
INSTALLED_APPS
, which Pants cannot read so I'm having to manually specify the apps as dependencies and I'm having trouble working out the right way to do it. I've tried specifying the directory like
src/app1
, and I've tried adding the individual
apps.py
files like
src/app1/core/apps.py
, which I've then overriden the dependencies of again to specify
src/app1/core/migrations
as a dependency of
src/app1/core/apps.py
. This is all in an attempt to get
manage.py migrate
to actually run the migrations it's supposed to, but it usually fails saying a particular app isn't installed, which means Django couldn't find the AppConfig class for it
w
What do your BUILD files look like?
f
So in src/app1/core I've got
Copy code
python_sources(
  dependencies=[
    './migrations',
  ],
)
In the migrations dir I also have this to include a directory of images, but I think that's by-the-by for my issue
Copy code
python_sources(
  dependencies=[':files'],
)

resources(
  name='files',
  sources=['files/**/*'],
)
And right now the web service BUILD looks something like this
Copy code
python_sources(
    name='lib',
    dependencies=[
        '//:reqs#django-cachalot',
        'src/barristan/apps.py',
        'src/app1/admin/apps.py',
        'src/app1/core/apps.py',
        'src/app2/apps.py',
    ],
    overrides={
        'manage.py': {
            'dependencies': [
              ':lib',
              '//:reqs#mysqlclient',
              './project/settings/local.py',
            ],
        },
    },
)
As mentioned above I've tried using
src/app1/core
inplace of the
apps.py
as well but no joy
w
Yeah, that's a bit of a confusing BUILD file. I didn't realize you could override something in the source to point to the source itself
Are you able to
pants peek ::
correctly? As in, how do all your deps look there
pants peek ::
has an absolute fudgeton of output, is there something specific I should be looking for?
I can see my migration files are listed as deps of apps.py
Ah if I add models as well as migrations it gets going
Will have to see if it can run all of 'em
w
👍
This is based on https://github.com/pantsbuild/example-django/blob/main/helloworld/service/welcome/BUILD
Yeah, it's locally qualified vs fully qualified to settings, but it just looks odd to me. Django seems to have it's own quirks. In general, @happy-kitchen-89482 might be a better person to talk about Django specifically, as he wrote a lot of the Django stuff/example. I use FastAPI and Alembic - and the way I structure everything is slightly different, but Django's "admin" capabilities are tightly coupled, so it makes sense there is some weirdness (from my perspective) in the build
After this is up and running, there may be a world where a macro can streamline a bunch of this for you
f
It makes sense that Pants didn't infer the models dependency because in Django you can't directly import models in migrations, you have to use
apps.get_model()
to preserve history
w
Ahh, I didnt know that
f
That's a pretty common pattern in Django specifically and if you know how the app names map to Python modules it's possible to translate
get_model
to its relevant python file
But it's not necessarily straightforward because Django's a bit loosey goosey when it comes to app names
w
Yeah, it's been like 12 years or something since I last touched Django 😆 I wonder if there is some reasonable way to "append" Django-specific dep inference on top of the existing python inference. As in, if this should be updated? https://github.com/pantsbuild/pants/blob/main/src/python/pants/backend/python/framework/django/dependency_inference.py
f
Oh hang on, that's a different framework…?
I've only got
pants.backend.python
in my pants.toml at the moment
…should I have been using
pants.backend.python.framework.django
this whole time?
w
Hmm, you know, I'm not sure - the example doesnt have it
But like, just pants-logic would make me assume..... yes?
f
It's
pants.backend.experimental.python.framework.django
in the docs. 🧑‍🔬
Well, I'm gonna try it
Though my migrations are running now after all that
w
https://www.pantsbuild.org/stable/docs/using-pants/key-concepts/backends#available-experimental-backends It's still experimental, so I'm not sure (though, there are a lot of our experimental backends which should be promoted to stable)
f
Django extension is picking up models and migrations correctly it seems. It still needs some help with my custom installed apps, but I suppose I can forgive that
It doesn't handle
django.urls.include
though
h
We used Pants with Django at scale pretty effectively at Toolchain (RIP), the repo (now oss) should show this: https://github.com/toolchainlabs/toolchain-oss
👍 1
gratitude thank you 1