I'm trying to make a minimal working example with ...
# general
e
I'm trying to make a minimal working example with
pants
, which would consist of one library and one app. The folder structure is
Copy code
šŸ“ pants-tests/
ā”œā”€šŸ“ apps/
│ ā””ā”€šŸ“ my-app/
│   ā”œā”€šŸ“„ pyproject.toml
│   ā”œā”€šŸ“„ BUILD
│   ā””ā”€šŸ“ myapp/
│     ā”œā”€šŸ“„ __init__.py
│     ā”œā”€šŸ“„ app.py
│     ā””ā”€šŸ“„ BUILD
ā”œā”€šŸ“„ .gitignore
ā”œā”€šŸ“ lib/
│ ā””ā”€šŸ“ my-pkg/
│   ā”œā”€šŸ“„ pyproject.toml
│   ā”œā”€šŸ“„ BUILD
│   ā””ā”€šŸ“ mypkg/
│     ā”œā”€šŸ“„ __init__.py
│     ā”œā”€šŸ“„ something.py
│     ā””ā”€šŸ“„ BUILD
ā””ā”€šŸ“„ pants.toml
how do you make
pants
and VS Code (or other IDEs) to know about the library without using virtual environments?
āœ… 1
My `pants.toml`:
Copy code
[GLOBAL]
pants_version = "2.16.0"
backend_packages = ["pants.backend.python"]

[source]
root_patterns = ['/lib', '/apps']
the `apps/my-app/pyproject.toml`:
Copy code
[project]
dependencies = ["my-pkg"]
name = "my-app"
requires-python = ">=3.10"
description = "An app"
version = "0.1.0"

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

[tool.setuptools.packages.find]
where = ["."]
include = ["myapp*"]
and `apps/my-app/BUILD`:
Copy code
python_requirements(source="pyproject.toml")
The app itself,
apps/my-app/myapp/app.py
Copy code
from mypkg.something import foo


def main():
    foo()


if __name__ == "__main__":
    main()
First problem is that
pants
does not see the dependency to `mypkg`:
Copy code
niko@niko-ubuntu:~/tmp/pants-tests$ pants dependencies apps/my-app/myapp/app.py 
14:57:23.29 [WARN] Pants cannot infer owners for the following imports in the target apps/my-app/myapp/app.py:

  * mypkg.something.foo (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.
niko@niko-ubuntu:~/tmp/pants-tests$ pants dependencies apps/my-app/myapp/app.py 
niko@niko-ubuntu:~/tmp/pants-tests$ pants dependencies --transitive apps/my-app/myapp/app.py 
niko@niko-ubuntu:~/tmp/pants-tests$
Second problem is that VS Code / Pylance does not see the dependency (naturally, as it is not installed and there is no venv):
r
Hey I would recommend having a look at the example repo https://www.pantsbuild.org/docs/example-repos
āœ… 1
Setting up IDE support is basically pointing your IDE to virtual env managed by pants. Have a look here https://www.pantsbuild.org/docs/setting-up-an-ide
āœ… 1
In your posted example, I would start with a single
pyproject.toml
and define all dependencies there.
That
pyproject.toml
can live alongside
pants.toml
e
I see what you mean by single
pyproject.toml
, but after this dummy example, I'm trying to make pants work with project with multiple apps and libraries. Then each app might have very different requirements.
r
It doesn't matter. You can have a single pyproject and depending on what 3rd party packages you are including in a lib/wheel, pants will only pick up required dependencies on its own. You only need to tell Pants your repo wide dependency universe. The place where you might need separate pyproject would be when you need to use different version of same 3rd party package. I would still try to first go with single one and build on top of that when things work for you
e
Thank you @gentle-florist-96289 ! I changed my example to be a bit more realistic, and continuing here: https://pantsbuild.slack.com/archives/C046T6T9U/p1691765287982279 I already got the 1st party library imports working nicely with VS Code, thanks to the link you posted!