I have a Python executable that can dynamically im...
# general
n
I have a Python executable that can dynamically import a number of other Python libraries using
importlib
. Since the imports are dynamic Pants doesn't know about the dependencies so I need to specify them manually in the
pex_binary
or
python_sources
. However, I want to depend on all the libraries in all subdirectories of a given directory without having to manually list them all. I get errors if I use something like
dependencies=['path/to/dir/::']
that say, "The address... from the
dependencies
field from the target <target> ended in a wildcard (
::
), which is not supported." Is there a way to add a dependency on all subdirectories?
h
Unfortunately there isn't. However, note that Pants can infer dependencies based on string literals, for example. Or you can write your own custom dep inference. How are the package names provided to the dynamic import?
n
Copy code
def import_module(root: Path, module: str) -> ModuleType:
    imported = importlib.import_module(f'deploys.{module}')
    return imported
(mid-refactor so
root
is currently ignored - long story)
So I need all subdirectories under
deploys
to be dependencies.
h
Ah, so there's no string literal to hang off
Hrm
alas, right now there is no way to glob over deps
it's not quite as trivial as it seems because of caching
n
bummer.
h
But it's not impossible, since
sources=
can be globs
n
How about this: could I create a separate pants build (e.g. a sibling directory with a
pants.toml
,
BUILD
, etc.) so I have 2 pants projects and one depends on the other (from the local disk, not pypi or other repo)?
oh! using
sources
might work...
h
Well, just because two files are in the
sources
of some target doesn't mean Pants will make them depend on each other
Unless, I think there may be a setting for that
One sec
n
Are recursive globs (e.g.
***/**.py
supported)? I don't see that in the docs.
The option of having it be a 2nd complete pants build but the dependency is a local, on disk dependency is appealing if that's possible.
h
They are
Recursive globs are supported, so I guess you can have a single
deploys
target that globs over everything, and then a manual dep on it from the calling code
you can't have cross pants project deps
that's not a thing
n
hmm... OK. Thanks a ton for your help. Gonna have to think on this for a bit...
c
We had to do a similar thing. The things we needed to import didn't change, so we could just make a
target
and add the output of
pants list my/dir::
as dependencies. Or something like that, it was a while ago
c
We use this spiffy tick to do something recursive dependency target globs: https://pantsbuild.slack.com/archives/C046T6T9U/p1708633247357399
🔥 1
❤️ 1