I have also noticed that the dependencies can't be...
# general
a
I have also noticed that the dependencies can't be resolved even though it mentions in the requirement.txt file
f
The generated sources are kept in the Pants cache and are "materialized" into the sandbox used to execute your Python code when you invoke pants.
If you want the IDE to see those sources, you will have to export them. I suggest using
./pants export
to produce a virtual environment from your code and requirements and point the IDE at it.
And since you are using generated sources, you will want to configure the
--export-py-generated-sources-in-resolve
option so the generated sources are also written to the venv. https://www.pantsbuild.org/stable/reference/goals/export#py_generated_sources_in_resolve
a
@fast-nail-55400 Thanks for your response. In the folder
~/.cache/pants
I don't have the folder
pants_dev_deps
. Instead that /home/acme/.cache/pants ❯ ls lmdb_store named_caches Am I doing something wrong?
How to configure [export] py_generated_sources_in_resolve = [ '<str>', '<str>', ..., ] What needs to be replaced '<str>', '<str>', ?
Running
❯ pants export resolve
Unknown goal: resolve Use
pants help
to get help. Use
pants help goals
to list goals.
How to use the export goal correctly?
I probably don't understand how this https://www.pantsbuild.org/stable/reference/goals/export can be used.
f
Are you familiar with the Pants "resolve" feature? (If you aren't using multiple resolves and just one set of requirements, then the name of the default Python resolve is
python-default
)
so to export a venv for
python-default
, you would run
pants export --resolve=python-default
and for the
py_generated_sources_in_resolve
option you would put
python-default
as the name of the resolve for which you wanted to include generated sources
See the docs on lock files for more on resolves: https://www.pantsbuild.org/stable/docs/python/overview/lockfiles
I don't recall if you need to opt in to resolves. See https://www.pantsbuild.org/stable/docs/python/overview/lockfiles#getting-started-with-resolves for details.
a
Dear @fast-nail-55400, Thank you so much for your help. I have done what you have described above. My pants.toml file looks as follow: [GLOBAL] pants_version = "2.22.0" backend_packages = [ "pants.backend.python", "pants.backend.python.lint.pylint", "pants.backend.codegen.protobuf.python", "pants.backend.codegen.protobuf.lint.buf", ] [python] interpreter_constraints = [">=3.11,<3.13"] enable_resolves = true [python-protobuf] mypy_plugin = true [export] py_generated_sources_in_resolve = ['python-default'] Running
pants export --resolve=python-default
it generates:
Copy code
pants export --resolve=python-default
13:00:32.80 [INFO] Completed: Build pex for resolve `python-default`
13:01:20.03 [INFO] Completed: Get interpreter version
13:01:20.85 [INFO] Completed: Building mypy_protobuf.pex from <resource://pants.backend.codegen.protobuf.python/mypy_protobuf.lock>
Wrote mutable virtualenv for python-default (using Python 3.12.5) to dist/export/python/virtualenvs/python-default/3.12.5
As you can see on the output above, the virtualenv has been created in the different folder instead in the
.~/.cache/pants
Is this ok?
f
Yes, that is fine. The Pants output directory for exports defaults to
dist/
(as does output from
pants package
as well for example)
You should now be able to set
PYTHONPATH
in a
.env
file in root of your repo to include that venv so VSCode can see the exported venv.
a
@fast-nail-55400 Thanks so much for your help. It works perfectly. To be honest, it was difficult to figure out to make it work. The documentation is not beginner friendly. Maybe we should improve the document for beginners. Thanks a lot
f
Maybe we should improve the document for beginners.
I agree. Would you like to contribute the PR? 🙂 Should be straightforward to do since the docs are just markdown files in the repository at https://github.com/pantsbuild/pants/tree/main/docs/docs.
a
Dear @fast-nail-55400, Yes, I will do it. 👍
p
@able-petabyte-69664 Would you mind sharing your
3rdparty/python
contents please? I have
BUILD
and
requirements.txt
there, and an empty
default.lock
. When I run
pants export --resolve=python-default
the resulting venv under
dist/export/python/virtualenvs/python-default/3.12.7
doesn't contain the libraries in my
requirements.txt
file
Ah - the step I was missing was
pants generate-lockfiles
- after doing this, my venv contains the libraries