What might be the problem when trying to check `de...
# general
e
What might be the problem when trying to check
dependencies
of a python file in a simple dummy project? (posting details to comments)
βœ… 1
The project structure is
Copy code
πŸ“ pants-tests/
β”œβ”€πŸ“„ .gitignore
β”œβ”€πŸ“ lib/
β”‚ β””β”€πŸ“ my-pkg/
β”‚   β”œβ”€πŸ“„ pyproject.toml
β”‚   β””β”€πŸ“ mypkg/
β”‚     β”œβ”€πŸ“„ __init__.py
β”‚     β”œβ”€πŸ“„ something.py
β”‚     β””β”€πŸ“„ BUILD
β””β”€πŸ“„ pants.toml
And the
pyproject.toml
defines "numba" as dependency
Copy code
[project]
name = "my-pkg"
requires-python = ">=3.10"
description = "A package"
version = "0.1.0"
dependencies = ["numba"]

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"


[tool.setuptools.packages.find]
where = ["."]
include = ["mypkg*"]
The
lib/my-pkg/mypkg/something.py
imports numba (and matplotlib):
Copy code
import matplotlib
import numba


def foo():
    print(matplotlib)
    print(numba)


if __name__ == "__main__":
    print("Hello from ", __file__)
and I'm using pants 2.15.0
When running
pants tailor ::
it creates
lib/my-pkg/mypkg/BUILD
with following contents:
Copy code
python_sources()
That is the only
BUILD
file created.
When trying to check the dependencies, I see nothing
Copy code
niko@niko-ubuntu:~/tmp/pants-tests$ pants dependencies lib/my-pkg/mypkg/something.py 
14:13:43.36 [WARN] Pants cannot infer owners for the following imports in the target lib/my-pkg/mypkg/something.py:

  * matplotlib (line: 1)
  * numba (line: 2)

If you do not expect an import to be inferrable, add `# pants: no-infer-dep` to the import line. Otherwise, see <https://www.pantsbuild.org/v2.15/docs/troubleshooting#import-errors-and-missing-dependencies> for common problems.
niko@niko-ubuntu:~/tmp/pants-tests$ pants dependencies --transitive lib/my-pkg/mypkg/something.py 
niko@niko-ubuntu:~/tmp/pants-tests$ pants dependencies lib/my-pkg/mypkg/something.py 
niko@niko-ubuntu:~/tmp/pants-tests$
Should the
BUILD
have some (other) content? I would expect that "numba" is added automatically as 3rd party dependency as it is listed in
pyproject.toml
. That seems not be the case. Is there a way to get the dependencies listed automatically? If not, where I should put manually put them?
Reading from the pants tailor docs it says:
Copy code
Auto-generate BUILD file targets for new source files.
from which I would assume the 3rd party dependencies are added to the BUILD files automatically by
pants tailor ::
. Why I don't see it? It does not specifically say if it reads the dependencies from setup.py or pyproject.toml or requirements.txt, or directly from import statements from the source files.. πŸ€”
b
Your version of pants may not support tailor-ing for that PEP621-style of pyproject.toml Thus, you may have to add a BUILD adjacent to that toml file with a
python_requirements
target in it. See the β€œpep 621” section of https://www.pantsbuild.org/docs/python-third-party-dependencies for more (I’m on mobile so can’t link to it directly)
e
Sorry I forgot to add, I am using pants 2.15.0
b
For more background: pants will infer dependencies in .py files, but only on to requirements that have been explicitly loaded into pants’ universe of understanding. This means, for instance, an explicit
python_requirements
target is required
πŸ‘ 1
Ah, maybe it doesn’t even support that style of file at all. Can you bump back up to 2.16?
e
Yeah, sure! I'll switch to 2.16.0
On pants 2.16.0 the results are quite much identical. The only
BUILD
file created by
pants tailor ::
has same contents and here is what the dependency check looks like
Copy code
niko@niko-ubuntu:~/tmp/pants-tests$ pants dependencies lib/my-pkg/mypkg/something.py 
14:31:59.42 [WARN] DEPRECATED: the factory default interpreter constraints value is scheduled to be removed in version 2.17.0.dev0.

You're relying on the default interpreter constraints that ship with Pants (('CPython>=3.7,<4',)). This default is deprecated, in favor of explicitly specifying the interpreter versions your code is actually intended to run against.

You specify interpreter constraints using the `interpreter_constraints` option in the `[python]` section of pants.toml. We recommend constraining to a single interpreter minor version if you can, e.g., `interpreter_constraints = ['==3.11.*']`, or at least a small number of interpreter minor versions, e.g., `interpreter_constraints = ['>=3.10,<3.12']`. See <https://www.pantsbuild.org/v2.16/docs/python-interpreter-compatibility> for details.

Set explicit interpreter constraints now to get rid of this warning.
14:32:07.38 [WARN] Pants cannot infer owners for the following imports in the target lib/my-pkg/mypkg/something.py:

  * matplotlib (line: 1)
  * numba (line: 2)

If you do not expect an import to be inferrable, add `# pants: no-infer-dep` to the import line. Otherwise, see <https://www.pantsbuild.org/v2.16/docs/troubleshooting#import-errors-and-missing-dependencies> for common problems.
niko@niko-ubuntu:~/tmp/pants-tests$ pants dependencies lib/my-pkg/mypkg/something.py 
niko@niko-ubuntu:~/tmp/pants-tests$
Thanks @broad-processor-92400, I changed to 2.16.0 and added manually a
BUILD
file next to the
pyproject.toml
:
Copy code
πŸ“ pants-tests/
β”œβ”€πŸ“„ .gitignore
β”œβ”€πŸ“ lib/
β”‚ β””β”€πŸ“ my-pkg/
β”‚   β”œβ”€πŸ“„ pyproject.toml
β”‚   β”œβ”€πŸ“„ BUILD    <----- manually added
β”‚   β””β”€πŸ“ mypkg/
β”‚     β”œβ”€πŸ“„ __init__.py
β”‚     β”œβ”€πŸ“„ something.py
β”‚     β””β”€πŸ“„ BUILD   <---- from pants tailor (automatic)
β””β”€πŸ“„ pants.toml
The
lib/my-pkg/BUILD
has only this as content:
Copy code
python_requirements(source="pyproject.toml")
Now the dependencies are shown correctly:
Copy code
niko@niko-ubuntu:~/tmp/pants-tests$ pants dependencies lib/my-pkg/mypkg/something.py 
14:35:12.88 [WARN] Pants cannot infer owners for the following imports in the target lib/my-pkg/mypkg/something.py:

  * matplotlib (line: 1)

If you do not expect an import to be inferrable, add `# pants: no-infer-dep` to the import line. Otherwise, see <https://www.pantsbuild.org/v2.16/docs/troubleshooting#import-errors-and-missing-dependencies> for common problems.
lib/my-pkg#numba
The
matplotlib
I purposefully did not add, so I could see what
pants
does automatically. On second run it looks like this:
Copy code
niko@niko-ubuntu:~/tmp/pants-tests$ pants dependencies lib/my-pkg/mypkg/something.py 
lib/my-pkg#numba
πŸŽ‰ 1
thanks a lot for the help!
b
No worries! Time for me to head off, but hopefully others can assist with any more questions you have!