https://pantsbuild.org/ logo
#general
Title
# general
e

echoing-farmer-15630

01/10/2023, 2:17 PM
I think while I've been away from
pants
, the dependency analyzer got too good 🙂 Silly problem: I'm trying to extract a subset of a library which tried to hide its dependencies by putting imports under
if
statements, which I think used to work (but I may be wrong) ie something like:
Copy code
def hide_imports(library: str):
    if library == "numpy":
        import numpy as result
    elif library == "pandas":
        import pandas as result
    return result
but
pants
cannot be fooled:
Copy code
❯ ./pants dependencies --no-dependencies-transitive src/.../test.py
//:root_requirements#numpy
//:root_requirements#pandas
...
(the situation is more complex with internal dependencies, but you get the idea; what if we wanted to build a library which only included
numpy
as a dependency and would be ok if someone called
hide_imports("pandas")
?) Is there any way to build a
python_distribution
or
pex_binary
with "only obvious" dependencies enclosed or expressly excluding some dependencies? Trying to build a subset of a library for a client without exposing more IP than necessary.
b

bitter-ability-32190

01/10/2023, 2:21 PM
You have options... 1. In the source code on the import line you don't want inferred add
# pants: no-infer-dep
2. In the BUILD file declare a dependency for the file and use an exclusion: e.g.
!//:root-reqs#pandas
(note the exclamation mark) 3. Muck up your source code import so pants doesn't know what's going on (mostly a joke 😌)
e

echoing-farmer-15630

01/10/2023, 10:36 PM
Awesome, and thanks for the clarification; I had even seen the
no-infer-dep
business earlier but somehow missed the comment tag (and knew about the exclusion but thought it would just break things somehow). Thanks very much for the help!